diff options
Diffstat (limited to 'platform')
6 files changed, 189 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 7cc1d003df..9c36343051 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -76,10 +76,13 @@ public final class MapboxMap { @Nullable private MapboxMap.OnFpsChangedListener onFpsChangedListener; + private com.mapbox.mapboxsdk.maps.Style style; + MapboxMap(NativeMapView map, Transform transform, UiSettings ui, Projection projection, OnGesturesManagerInteractionListener listener, AnnotationManager annotations, CameraChangeDispatcher cameraChangeDispatcher, MapChangeReceiver mapChangeReceiver) { this.nativeMapView = map; + this.style = new com.mapbox.mapboxsdk.maps.Style(nativeMapView); this.uiSettings = ui; this.projection = projection; this.annotationManager = annotations.bind(this); @@ -101,6 +104,10 @@ public final class MapboxMap { setPrefetchesTiles(options); } + public com.mapbox.mapboxsdk.maps.Style getStyle(){ + return style; + } + /** * Called when the hosting Activity/Fragment onStart() method is called. */ diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java new file mode 100644 index 0000000000..50192d8f8d --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java @@ -0,0 +1,71 @@ +package com.mapbox.mapboxsdk.maps; + +import com.mapbox.mapboxsdk.style.layers.Layer; +import com.mapbox.mapboxsdk.style.sources.Source; + +import java.util.HashMap; + +public class Style { + + private NativeMapView nativeMapView; + private final HashMap<String, Source> sources = new HashMap<>(); + private final HashMap<String, Layer> layers = new HashMap<>(); + + public Style(NativeMapView nativeMapView) { + this.nativeMapView = nativeMapView; + } + + public void loadStyle(@com.mapbox.mapboxsdk.constants.Style.StyleUrl String styleUrl) { + for (Source source : sources.values()) { + if (source != null) { + source.setDetached(); + nativeMapView.removeSource(source); + } + } + + for (Layer layer : layers.values()) { + if (layer != null) { + layer.setDetached(); + nativeMapView.removeLayer(layer); + } + } + + nativeMapView.setStyleUrl(styleUrl); + } + + // + // Source + // + + public void addSource(Source source) { + sources.put(source.getId(), source); + nativeMapView.addSource(source); + } + + public Source getSource(String id) { + Source source = sources.get(id); + if (source == null) { + source = nativeMapView.getSource(id); + } + return source; + } + + // + // Layer + // + + public void addLayer(Layer layer) { + layers.put(layer.getId(), layer); + nativeMapView.addLayer(layer); + } + + public Layer getLayer(String id) { + Layer layer = layers.get(id); + if (layer == null) { + layer = nativeMapView.getLayer(id); + } + return layer; + } + + +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java index 72591196d4..75388ec32c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/layers/Layer.java @@ -18,6 +18,7 @@ public abstract class Layer { private long nativePtr; @Keep private boolean invalidated; + private boolean detached; @Keep protected Layer(long nativePtr) { @@ -37,6 +38,10 @@ public abstract class Layer { } public void setProperties(@NonNull PropertyValue<?>... properties) { + if (detached) { + return; + } + checkThread(); if (properties.length == 0) { return; @@ -146,4 +151,12 @@ public abstract class Layer { return value; } } + + public void setDetached() { + detached = true; + } + + public boolean isDetached() { + return detached; + } }
\ No newline at end of file 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 a9eb43f968..c59cfeaf0b 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 @@ -194,6 +194,9 @@ public class GeoJsonSource extends Source { * @param feature the GeoJSON {@link Feature} to set */ public void setGeoJson(Feature feature) { + if (detached) { + return; + } checkThread(); nativeSetFeature(feature); } @@ -205,6 +208,9 @@ public class GeoJsonSource extends Source { * @param geometry the GeoJSON {@link Geometry} to set */ public void setGeoJson(Geometry geometry) { + if (detached) { + return; + } checkThread(); nativeSetGeometry(geometry); } @@ -216,6 +222,9 @@ public class GeoJsonSource extends Source { * @param features the GeoJSON FeatureCollection */ public void setGeoJson(FeatureCollection features) { + if (detached) { + return; + } checkThread(); nativeSetFeatureCollection(features); } @@ -227,6 +236,9 @@ public class GeoJsonSource extends Source { * @param json the raw GeoJson FeatureCollection string */ public void setGeoJson(String json) { + if (detached) { + return; + } checkThread(); nativeSetGeoJsonString(json); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/Source.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/Source.java index 44f4a6c344..6e8c521a78 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/Source.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/Source.java @@ -13,6 +13,8 @@ public abstract class Source { @Keep private long nativePtr; + protected boolean detached; + /** * Internal use * @@ -76,4 +78,8 @@ public abstract class Source { @NonNull @Keep protected native String nativeGetAttribution(); + + public void setDetached() { + detached = true; + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/style/CustomGeometryTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/style/CustomGeometryTest.kt new file mode 100644 index 0000000000..dbbc590b9b --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/style/CustomGeometryTest.kt @@ -0,0 +1,80 @@ +package com.mapbox.mapboxsdk.style + +import android.support.test.espresso.UiController +import com.mapbox.geojson.Feature +import com.mapbox.geojson.FeatureCollection +import com.mapbox.geojson.Point +import com.mapbox.mapboxsdk.constants.Style +import com.mapbox.mapboxsdk.maps.MapboxMap +import com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage +import com.mapbox.mapboxsdk.style.layers.SymbolLayer +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource +import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke +import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest +import com.mapbox.mapboxsdk.testapp.activity.maplayout.SimpleMapActivity +import org.junit.Test + +class CustomGeometrySourceTest : BaseActivityTest() { + + override fun getActivityClass(): Class<*> = SimpleMapActivity::class.java + + @Test + fun sourceUpdateAfterStyleChange() { + validateTestSetup() + invoke(mapboxMap) { uiController: UiController, mapboxMap: MapboxMap -> + val style = mapboxMap.style + + val source = GeoJsonSource( + "source-id", + FeatureCollection.fromFeature( + Feature.fromGeometry( + Point.fromLngLat(0.0, 0.0) + ) + ) + ) + style.addSource(source) + + style.loadStyle(Style.DARK) + + uiController.loopMainThreadForAtLeast(300) + + source.setGeoJson( + FeatureCollection.fromFeature( + Feature.fromGeometry( + Point.fromLngLat(1.0, 1.0) + ) + ) + ) + } + } + + @Test + fun layerUpdateAfterStyleChange() { + validateTestSetup() + invoke(mapboxMap) { uiController: UiController, mapboxMap: MapboxMap -> + val style = mapboxMap.style + + val source = GeoJsonSource( + "source-id", + FeatureCollection.fromFeature( + Feature.fromGeometry( + Point.fromLngLat(0.0, 0.0) + ) + ) + ) + style.addSource(source) + + val layer = SymbolLayer("test", "source-id") + style.addLayer(layer) + + style.loadStyle(Style.DARK) + + layer.setProperties(iconImage("bus-15")) + + uiController.loopMainThreadForAtLeast(300) + + layer.setProperties(iconImage("bus-15")) + } + } + +}
\ No newline at end of file |