-
-
Save domadev812/dffb6d76540c7eb654b05fc277258ede to your computer and use it in GitHub Desktop.
Publishing Android Location on a Live-Updating Map (MapBox)
This file contains 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
<dependency> | |
<groupId>com.pubnub</groupId> | |
<artifactId>pubnub-gson</artifactId> | |
<version>4.14.0</version> | |
</dependency> |
This file contains 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 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 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) { | |
[ . . . ] | |
PNConfiguration pnConfiguration = new PNConfiguration(); | |
pnConfiguration.setSubscribeKey("demo"); | |
pnConfiguration.setPublishKey("demo"); | |
mPubnub = new PubNub(pnConfiguration); | |
try { | |
mPubnub.subscribe() | |
.channels(Arrays.asList("my_channel")) // subscribe to channels | |
.execute(); | |
mPubnub.addListener(subscribeCallback); | |
} catch (PubnubException e) { | |
Log.e(TAG, e.toString()); | |
} | |
} |
This file contains 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
SubscribeCallback subscribeCallback = new SubscribeCallback() { | |
@Override | |
public void status(PubNub pubnub, PNStatus status) { | |
} | |
@Override | |
public void message(PubNub pubnub, PNMessageResult message) { | |
Log.d(PUBNUB_TAG, "Message Received: " + message.toString()); | |
} | |
@Override | |
public void presence(PubNub pubnub, PNPresenceEventResult presence) { | |
} | |
}; |
This file contains 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 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 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 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 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