diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2016-10-20 15:45:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-20 15:45:57 +0200 |
commit | 04a9a98b98d41312a687518fd43445a64dadadf0 (patch) | |
tree | 05f3809f194d18264ae8ca45137f0cdb9ae120a4 /platform/android/MapboxGLAndroidSDKTestApp/src | |
parent | ab63a9494f109245f9bd81839e2576b60a34cd2c (diff) | |
download | qtlocation-mapboxgl-04a9a98b98d41312a687518fd43445a64dadadf0.tar.gz |
[android] - add realtime geojson example from gl-js (#6751)
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src')
4 files changed, 169 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 2fd973ee82..c8a0de9826 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -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" diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RealTimeGeoJsonActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RealTimeGeoJsonActivity.java new file mode 100644 index 0000000000..debd44fb1f --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RealTimeGeoJsonActivity.java @@ -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); + } + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_default.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_default.xml new file mode 100644 index 0000000000..742f6612bd --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_default.xml @@ -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> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index 3d5bf863d8..a4f19f1e01 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -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> |