diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2016-08-02 23:23:11 -0400 |
---|---|---|
committer | Ivo van Dongen <info@ivovandongen.nl> | 2016-08-13 11:20:08 -0400 |
commit | 743d134a32161e3bfb7ea0c9042094aee5aede2b (patch) | |
tree | 675b1c6f92b34489e913ba4682a72713ccffd87c | |
parent | 112789a76111d4dd38e50c73ae1e81e5c53dd59c (diff) | |
download | qtlocation-mapboxgl-743d134a32161e3bfb7ea0c9042094aee5aede2b.tar.gz |
[android] #5852 - geojson source clustering
7 files changed, 279 insertions, 16 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java index fc7928015e..6f2113ab0e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java @@ -41,4 +41,19 @@ public class GeoJsonSource extends Source { super(id, TYPE); this.put(DATA_KEY, url.toExternalForm()); } + + public GeoJsonSource withCluster(boolean cluster) { + this.put("cluster", cluster); + return this; + } + + public GeoJsonSource withClusterMaxZoom(float zoom) { + this.put("clusterMaxZoom", zoom); + return this; + } + + public GeoJsonSource withClusterRadius(float radius) { + this.put("clusterRadius", radius); + return this; + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index cafca62135..db1fa025ae 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -309,6 +309,15 @@ android:value="@string/category_style" /> </activity> <activity + android:name=".activity.style.GeoJsonClusteringActivity" + android:description="@string/description_geojson_clustering" + android:label="@string/activity_geojson_clustering"> + <meta-data + android:name="@string/category" + android:value="@string/category_style" /> + </activity> + + <activity android:name=".activity.imagegenerator.PrintActivity" android:description="@string/description_print" android:label="@string/activity_print"> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GeoJsonClusteringActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GeoJsonClusteringActivity.java new file mode 100644 index 0000000000..65ccbf4c40 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GeoJsonClusteringActivity.java @@ -0,0 +1,202 @@ +package com.mapbox.mapboxsdk.testapp.activity.style; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.annotation.RawRes; +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.Menu; +import android.view.MenuItem; +import android.widget.Toast; + +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.style.layers.CircleLayer; +import com.mapbox.mapboxsdk.style.layers.FillLayer; +import com.mapbox.mapboxsdk.style.layers.Function; +import com.mapbox.mapboxsdk.style.layers.Layer; +import com.mapbox.mapboxsdk.style.layers.LineLayer; +import com.mapbox.mapboxsdk.style.layers.NoSuchLayerException; +import com.mapbox.mapboxsdk.style.layers.Property; +import com.mapbox.mapboxsdk.style.layers.PropertyValue; +import com.mapbox.mapboxsdk.style.layers.RasterLayer; +import com.mapbox.mapboxsdk.style.layers.SymbolLayer; +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; +import com.mapbox.mapboxsdk.style.sources.RasterSource; +import com.mapbox.mapboxsdk.style.sources.Source; +import com.mapbox.mapboxsdk.style.sources.TileSet; +import com.mapbox.mapboxsdk.style.sources.VectorSource; +import com.mapbox.mapboxsdk.testapp.R; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.net.MalformedURLException; +import java.net.URL; + +import static com.mapbox.mapboxsdk.style.layers.Filter.all; +import static com.mapbox.mapboxsdk.style.layers.Filter.eq; +import static com.mapbox.mapboxsdk.style.layers.Filter.gte; +import static com.mapbox.mapboxsdk.style.layers.Filter.lt; +import static com.mapbox.mapboxsdk.style.layers.Function.Stop; +import static com.mapbox.mapboxsdk.style.layers.Function.stop; +import static com.mapbox.mapboxsdk.style.layers.Function.zoom; +import static com.mapbox.mapboxsdk.style.layers.Property.FILL_TRANSLATE_ANCHOR_MAP; +import static com.mapbox.mapboxsdk.style.layers.Property.NONE; +import static com.mapbox.mapboxsdk.style.layers.Property.SYMBOL_PLACEMENT_POINT; +import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.backgroundOpacity; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillAntialias; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOutlineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillTranslateAnchor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.symbolPlacement; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; + +/** + * Sample Activity to show off geojson source clustering and filter usage + */ +public class GeoJsonClusteringActivity extends AppCompatActivity { + private static final String TAG = GeoJsonClusteringActivity.class.getSimpleName(); + + private MapView mapView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_geojson_clustering); + + setupActionBar(); + + //Initialize map as normal + mapView = (MapView) findViewById(R.id.mapView); + //noinspection ConstantConditions + mapView.onCreate(savedInstanceState); + + + mapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(MapboxMap mapboxMap) { + mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.7749, 122.4194), 0)); + + //Add a clustered source with some layers + addClusteredGeoJsonSource(mapboxMap); + } + }); + } + + + @Override + public void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mapView.onDestroy(); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } + + + private void addClusteredGeoJsonSource(MapboxMap mapboxMap) { + //Add a clustered source + try { + mapboxMap.addSource( + new GeoJsonSource("earthquakes", new URL("https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson")) + .withCluster(true) + .withClusterMaxZoom(14) + .withClusterRadius(50) + ); + } catch (MalformedURLException e) { + Log.e(TAG, "That's not an url... " + e.getMessage()); + } + + //Add unclustered layer + int[][] layers = new int[][]{ + new int[]{150, Color.RED}, + new int[]{20, Color.GREEN}, + new int[]{0, Color.BLUE} + }; + + SymbolLayer unclustered = new SymbolLayer("unclustered-points", "earthquakes"); + unclustered.setProperties(iconImage("marker-15")); + mapboxMap.addLayer(unclustered); + + for (int i = 0; i < layers.length; i++) { + //Add some nice circles + CircleLayer circles = new CircleLayer("cluster-" + i, "earthquakes"); + circles.setProperties( + circleColor(layers[i][1]), + circleRadius(18f) + ); + circles.setFilter( + i == 0 ? + gte("point_count", layers[i][0]) : + all(gte("point_count", layers[i][0]), lt("point_count", layers[i - 1][0])) + ); + mapboxMap.addLayer(circles); + } + + //Add the count labels + SymbolLayer count = new SymbolLayer("count", "earthquakes"); + count.setProperties( + textField("{point_count}"), + textSize(12f) + ); + mapboxMap.addLayer(count); + + + //Zoom out to start + mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(1)); + } + + + private void setupActionBar() { + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowHomeEnabled(true); + } + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java index 71510d6868..481b0d7cec 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java @@ -39,13 +39,29 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; -import static com.mapbox.mapboxsdk.style.layers.Filter.*; -import static com.mapbox.mapboxsdk.style.layers.Function.*; -import static com.mapbox.mapboxsdk.style.layers.Property.*; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.*; +import static com.mapbox.mapboxsdk.style.layers.Filter.eq; +import static com.mapbox.mapboxsdk.style.layers.Function.Stop; +import static com.mapbox.mapboxsdk.style.layers.Function.stop; +import static com.mapbox.mapboxsdk.style.layers.Function.zoom; +import static com.mapbox.mapboxsdk.style.layers.Property.FILL_TRANSLATE_ANCHOR_MAP; +import static com.mapbox.mapboxsdk.style.layers.Property.NONE; +import static com.mapbox.mapboxsdk.style.layers.Property.SYMBOL_PLACEMENT_POINT; +import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.backgroundOpacity; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillAntialias; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOutlineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillTranslateAnchor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.symbolPlacement; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; /** - * Sample Activity to show a typical location picker use case + * Sample Activity to show off the runtime style api */ public class RuntimeStyleActivity extends AppCompatActivity { private static final String TAG = RuntimeStyleActivity.class.getSimpleName(); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_geojson_clustering.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_geojson_clustering.xml new file mode 100644 index 0000000000..4c0d067c14 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_geojson_clustering.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/menu/menu_runtime_style.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml index 189a33708f..16713ae147 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml @@ -1,45 +1,45 @@ <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:mapbox="http://schemas.android.com/apk/res-auto"> + xmlns:mapbox="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_water_color" android:title="Color the water" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_background_opacity" android:title="Set background opacity" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_road_avoid_edges" android:title="Set road symbol placement to Point" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_layer_visibility" android:title="Set layer visibility to false" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_add_parks_layer" android:title="Add a parks layer" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_remove_layer" android:title="Remove buildings layer" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_add_terrain_layer" android:title="Add a terrain layer" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_add_satellite_layer" android:title="Add a satellite layer" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_update_water_color_on_zoom" android:title="Change the water color on zoom" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> <item android:id="@+id/action_add_custom_tiles" android:title="Custom tiles" - mapbox:showAsAction="never"/> + mapbox:showAsAction="never" /> </menu>
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index 00c421bfef..1cdf449a6c 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -54,6 +54,7 @@ <string name="activity_minmax_zoom">Min/Max Zoom</string> <string name="activity_viewpager">ViewPager</string> <string name="activity_runtime_style">Runtime Style</string> + <string name="activity_geojson_clustering">GeoJson Clustering</string> <string name="activity_print">Print a map</string> <string name="activity_surfaceview_overlay">SurfaceView MediaOverlay</string> <string name="title_activity_navigation_drawer">Android SDK View integration</string> @@ -95,6 +96,7 @@ <string name="description_location_picker">Use a fixed Marker to select your location</string> <string name="description_viewpager">Use SupportMapFragments in a ViewPager</string> <string name="description_runtime_style">Adopt the map style on the fly</string> + <string name="description_geojson_clustering">Use GeoJson sources and dynamic layers to cluster information</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> |