Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from domadev812/1.bash
Created November 15, 2017 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/cb68afb671c30402a62af810068373b1 to your computer and use it in GitHub Desktop.
Save stephenlb/cb68afb671c30402a62af810068373b1 to your computer and use it in GitHub Desktop.
Publishing Android Location on a Live-Updating Map (MapBox)
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-gson</artifactId>
<version>4.14.0</version>
</dependency>
private void updateCamera() {
mMapView.setCameraDistance(10);
mMapView.setCenter(mLatLng);
}
<?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>
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());
}
}
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) {
}
};
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
[ . . . ]
}
private PathOverlay mLine;
private void initializePolyline() {
mLine = new PathOverlay(Color.BLUE, 5);
}
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();
}
});
}
};
private void updatePolyline() {
mMapView.removeOverlay(mLine);
mLine.addPoint(mLatLng);
mMapView.getOverlays().add(mLine);
}
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