diff options
author | Tobrun Van Nuland <tobrun.van.nuland@gmail.com> | 2017-09-13 13:11:43 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2017-09-14 10:55:52 +0200 |
commit | c795d96bfb980f8b60b9295b5182b2ee90893149 (patch) | |
tree | 7ffa0f44fa543ea3f9ed3737a17e2ab78e2b63b9 /platform/android/MapboxGLAndroidSDKTestApp/src/main/java | |
parent | d1354466f03a9a14097c349f65aa31ab457fce0a (diff) | |
download | qtlocation-mapboxgl-c795d96bfb980f8b60b9295b5182b2ee90893149.tar.gz |
[android] - update zoom function example with selected state
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java index ab33827e61..cd741a85b6 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java @@ -1,35 +1,46 @@ package com.mapbox.mapboxsdk.testapp.activity.style; +import android.graphics.PointF; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; +import com.google.gson.JsonObject; +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.SymbolLayer; import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; import com.mapbox.mapboxsdk.testapp.R; +import com.mapbox.services.commons.geojson.Feature; +import com.mapbox.services.commons.geojson.FeatureCollection; +import com.mapbox.services.commons.geojson.Point; +import com.mapbox.services.commons.models.Position; -import java.net.MalformedURLException; -import java.net.URL; +import java.util.List; import timber.log.Timber; +import static com.mapbox.mapboxsdk.style.functions.Function.property; import static com.mapbox.mapboxsdk.style.functions.Function.zoom; import static com.mapbox.mapboxsdk.style.functions.stops.Stop.stop; +import static com.mapbox.mapboxsdk.style.functions.stops.Stops.categorical; import static com.mapbox.mapboxsdk.style.functions.stops.Stops.interval; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; +/** + * Test activity showcasing changing the icon with a zoom function and adding selection state to a SymbolLayer. + */ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity { - private static final String SOURCE_URL = "https://gist.githubusercontent.com/anonymous/" - + "70ddad0e58520307cd1699e704f4b626/raw/a5deaccdc4517c12d63b1f8abcb7becf5e36506a/map.geojson"; private static final String LAYER_ID = "symbolLayer"; private static final String SOURCE_ID = "poiSource"; private static final String BUS_MAKI_ICON_ID = "bus-11"; private static final String CAFE_MAKI_ICON_ID = "cafe-11"; + private static final String KEY_PROPERTY_SELECTED = "selected"; private static final float ZOOM_STOP_MIN_VALUE = 7.0f; private static final float ZOOM_STOP_MAX_VALUE = 12.0f; @@ -48,19 +59,30 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity { @Override public void onMapReady(@NonNull final MapboxMap map) { mapboxMap = map; - addSource(); + updateSource(false); addLayer(); + addMapClickListener(); } }); } - private void addSource() { - try { - source = new GeoJsonSource(SOURCE_ID, new URL(SOURCE_URL)); - } catch (MalformedURLException exception) { - Timber.e(exception, "That's not an url... "); + private void updateSource(boolean selected) { + FeatureCollection featureCollection = createFeatureCollection(selected); + if (source != null) { + source.setGeoJson(featureCollection); + } else { + source = new GeoJsonSource(SOURCE_ID, featureCollection); + mapboxMap.addSource(source); } - mapboxMap.addSource(source); + } + + private FeatureCollection createFeatureCollection(boolean selected) { + Point point = Point.fromCoordinates(Position.fromCoordinates(-74.016181, 40.701745)); + Feature feature = Feature.fromGeometry(point); + JsonObject properties = new JsonObject(); + properties.addProperty(KEY_PROPERTY_SELECTED, selected); + feature.setProperties(properties); + return FeatureCollection.fromFeatures(new Feature[] {feature}); } private void addLayer() { @@ -74,11 +96,37 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity { ) ) ), + iconSize( + property( + KEY_PROPERTY_SELECTED, + categorical( + stop(true, iconSize(3.0f)), + stop(false, iconSize(1.0f)) + ) + ) + ), iconAllowOverlap(true) ); mapboxMap.addLayer(layer); } + private void addMapClickListener() { + mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() { + @Override + public void onMapClick(@NonNull LatLng point) { + PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point); + List<Feature> featureList = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID); + if (!featureList.isEmpty()) { + Feature feature = featureList.get(0); + boolean isSelected = feature.getBooleanProperty(KEY_PROPERTY_SELECTED); + updateSource(!isSelected); + } else { + Timber.e("No features found"); + } + } + }); + } + @Override protected void onStart() { super.onStart(); |