[android] #5852 - geojson source clustering
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,6 +308,15 @@
|
||||
android:name="@string/category"
|
||||
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"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user