[android] - add realtime geojson example from gl-js (#6751)
This commit is contained in:
@@ -348,6 +348,13 @@
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_style" />
|
||||
</activity>
|
||||
<activity android:name=".activity.style.RealTimeGeoJsonActivity"
|
||||
android:description="@string/description_geojson_realtime"
|
||||
android:label="@string/activity_geojson_realtime">
|
||||
<meta-data
|
||||
android:name="@string/category"
|
||||
android:value="@string/category_style" />
|
||||
</activity>
|
||||
<activity
|
||||
android:name=".activity.style.StyleFileActivity"
|
||||
android:description="@string/description_style_file"
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.style;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
|
||||
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
|
||||
|
||||
/**
|
||||
* Use realtime GeoJSON data streams to move a symbol on your map
|
||||
* <p>
|
||||
* GL-native equivalent of https://www.mapbox.com/mapbox-gl-js/example/live-geojson/
|
||||
* </p>
|
||||
*/
|
||||
public class RealTimeGeoJsonActivity extends AppCompatActivity implements OnMapReadyCallback {
|
||||
|
||||
private final static String ID_GEOJSON_LAYER = "wanderdrone";
|
||||
private final static String ID_GEOJSON_SOURCE = ID_GEOJSON_LAYER;
|
||||
private final static String URL_GEOJSON_SOURCE = "https://wanderdrone.appspot.com/";
|
||||
|
||||
private MapView mapView;
|
||||
private MapboxMap mapboxMap;
|
||||
|
||||
private Handler handler;
|
||||
private Runnable runnable;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_default);
|
||||
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
actionBar.setDisplayShowHomeEnabled(true);
|
||||
}
|
||||
|
||||
mapView = (MapView) findViewById(R.id.mapView);
|
||||
mapView.onCreate(savedInstanceState);
|
||||
mapView.getMapAsync(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMapReady(@NonNull final MapboxMap map) {
|
||||
mapboxMap = map;
|
||||
|
||||
// add source
|
||||
try {
|
||||
mapboxMap.addSource(new GeoJsonSource(ID_GEOJSON_SOURCE, new URL(URL_GEOJSON_SOURCE)));
|
||||
}catch (MalformedURLException e){
|
||||
Log.e(MapboxConstants.TAG, "Invalid URL", e);
|
||||
}
|
||||
|
||||
// add layer
|
||||
SymbolLayer layer = new SymbolLayer(ID_GEOJSON_LAYER, ID_GEOJSON_SOURCE);
|
||||
layer.setProperties(iconImage("rocket-15"));
|
||||
mapboxMap.addLayer(layer);
|
||||
|
||||
// loop refresh geojson
|
||||
handler = new Handler();
|
||||
runnable = new RefreshGeoJsonRunnable(mapboxMap, handler);
|
||||
handler.postDelayed(runnable, 2000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
handler.removeCallbacks(runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mapView.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
mapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
private static class RefreshGeoJsonRunnable implements Runnable {
|
||||
|
||||
private MapboxMap mapboxMap;
|
||||
private Handler handler;
|
||||
|
||||
RefreshGeoJsonRunnable(MapboxMap mapboxMap, Handler handler) {
|
||||
this.mapboxMap = mapboxMap;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
((GeoJsonSource) mapboxMap.getSource(ID_GEOJSON_SOURCE)).setUrl(URL_GEOJSON_SOURCE);
|
||||
handler.postDelayed(this, 2000);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
android:background="@color/primary"
|
||||
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
|
||||
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:id="@+id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@id/toolbar" />
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -60,6 +60,7 @@
|
||||
<string name="activity_runtime_style">Runtime Style</string>
|
||||
<string name="activity_style_file">Local Style file</string>
|
||||
<string name="activity_geojson_clustering">GeoJson Clustering</string>
|
||||
<string name="activity_geojson_realtime">Add live realtime data</string>
|
||||
<string name="activity_print">Print a map</string>
|
||||
<string name="activity_surfaceview_overlay">SurfaceView MediaOverlay</string>
|
||||
<string name="activity_query_rendered_feature_properties">Query feature properties</string>
|
||||
@@ -113,6 +114,7 @@
|
||||
<string name="description_circle">Use GeoJson source to show a circle</string>
|
||||
<string name="description_custom_sprite">Use a custom sprite in a Symbol Layer</string>
|
||||
<string name="description_geojson_clustering">Use GeoJson sources and dynamic layers to cluster information</string>
|
||||
<string name="description_geojson_realtime">Use realtime GeoJSON data streams to move a symbol on your map</string>
|
||||
<string name="description_print">Shows how to print a map</string>
|
||||
<string name="description_navigation_drawer">Test animation of Android SDK View components</string>
|
||||
<string name="description_surfaceview_mediacontrols">Test overlaying SurfaceView</string>
|
||||
|
||||
Reference in New Issue
Block a user