Created
May 8, 2015 22:28
-
-
Save ToeJamson/88a05aae657f84e491eb to your computer and use it in GitHub Desktop.
Publishing Android Location on a Live-Updating Map (MapBox)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git clone https://github.com/pubnub/java.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void updateCamera() { | |
mMapView.setCameraDistance(10); | |
mMapView.setCenter(mLatLng); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:orientation="vertical" > | |
<com.mapbox.mapboxsdk.views.MapView | |
android:id="@+id/mapview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:mapid="Your Map ID Here" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Pubnub mPubnub; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
[ . . . ] | |
mPubnub = new Pubnub("publish_Key", "subscribe_key"); | |
try { | |
mPubnub.subscribe("Channel Name Of Your Choice", subscribeCallback); | |
} catch (PubnubException e) { | |
Log.e(TAG, e.toString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Callback subscribeCallback = new Callback() { | |
@Override | |
public void successCallback(String channel, Object message) { | |
Log.d(PUBNUB_TAG, "Message Received: " + message.toString()); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private MapView mMapView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_mbox_view); | |
// Get MapView | |
mMapView = (MapView) findViewById(R.id.mapview); | |
initializePolyline(); | |
// Start PubNub | |
[ . . . ] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private PathOverlay mLine; | |
private void initializePolyline() { | |
mLine = new PathOverlay(Color.BLUE, 5); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private LatLng mLatLng; | |
Callback subscribeCallback = new Callback() { | |
@Override | |
public void successCallback(String channel, Object message) { | |
JSONObject jsonMessage = (JSONObject) message; | |
try { | |
double mLat = jsonMessage.getDouble("lat"); | |
double mLng = jsonMessage.getDouble("lng"); | |
mLatLng = new LatLng(mLat, mLng); | |
} catch (JSONException e) { | |
Log.e(TAG, e.toString()); | |
} | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
updatePolyline(); | |
updateCamera(); | |
updateMarker(); | |
} | |
}); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void updatePolyline() { | |
mMapView.removeOverlay(mLine); | |
mLine.addPoint(mLatLng); | |
mMapView.getOverlays().add(mLine); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean isFirstMessage = true; | |
private void updateMarker() { | |
if (!isFirstMessage) { | |
mMapView.removeMarker(mMarker); | |
} | |
isFirstMessage = false; | |
mMarker = new Marker(mMapView, "", "", mLatLng); | |
mMapView.addMarker(mMarker); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment