From f701a6ae6297c6013817f616ba14b4b1363eb016 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Thu, 8 Oct 2020 14:03:19 +0200 Subject: [PATCH] [location] validate if style is still loading (#591) --- .../location/SymbolLocationLayerRenderer.java | 23 +++++++++++++++++++ .../location/LocationLayerControllerTest.java | 1 + 2 files changed, 24 insertions(+) diff --git a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/SymbolLocationLayerRenderer.java b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/SymbolLocationLayerRenderer.java index bd6151617..dae5069c9 100644 --- a/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/SymbolLocationLayerRenderer.java +++ b/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/SymbolLocationLayerRenderer.java @@ -11,6 +11,7 @@ import com.mapbox.geojson.Point; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.location.modes.RenderMode; +import com.mapbox.mapboxsdk.log.Logger; import com.mapbox.mapboxsdk.maps.Style; import com.mapbox.mapboxsdk.style.expressions.Expression; import com.mapbox.mapboxsdk.style.layers.Layer; @@ -59,6 +60,8 @@ import static com.mapbox.mapboxsdk.utils.ColorUtils.colorToRgbaString; final class SymbolLocationLayerRenderer implements LocationLayerRenderer { + + private static final String TAG = "mbgl-locationSymbol"; private Style style; private final LayerSourceProvider layerSourceProvider; @@ -179,6 +182,11 @@ public void setAccuracyRadius(Float accuracy) { @Override public void styleScaling(Expression scaleExpression) { + if (!style.isFullyLoaded()) { + Logger.w(TAG, "Style is not fully loaded, not able to get layer!"); + return; + } + for (String layerId : layerSet) { Layer layer = style.getLayer(layerId); if (layer instanceof SymbolLayer) { @@ -244,6 +252,11 @@ private void updateForegroundBearing(float bearing) { } private void setLayerVisibility(@NonNull String layerId, boolean visible) { + if (!style.isFullyLoaded()) { + Logger.w(TAG, "Style is not fully loaded, not able to get layer!"); + return; + } + Layer layer = style.getLayer(layerId); if (layer != null) { String targetVisibility = visible ? VISIBLE : NONE; @@ -266,6 +279,11 @@ public void adjustPulsingCircleLayerVisibility(boolean visible) { */ @Override public void stylePulsingCircle(LocationComponentOptions options) { + if (!style.isFullyLoaded()) { + Logger.w(TAG, "Style is not fully loaded, not able to get layer!"); + return; + } + if (style.getLayer(PULSING_CIRCLE_LAYER) != null) { setLayerVisibility(PULSING_CIRCLE_LAYER, true); style.getLayer(PULSING_CIRCLE_LAYER).setProperties( @@ -318,6 +336,11 @@ private void addLocationSource() { } private void refreshSource() { + if (!style.isFullyLoaded()) { + Logger.w(TAG, "Style is not fully loaded, not able to get source!"); + return; + } + GeoJsonSource source = style.getSourceAs(LOCATION_SOURCE); if (source != null) { locationSource.setGeoJson(locationFeature); diff --git a/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java b/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java index db66dfc8c..f95d6fdf5 100644 --- a/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java +++ b/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationLayerControllerTest.java @@ -65,6 +65,7 @@ public class LocationLayerControllerTest { @Before public void before() { when(mapboxMap.getStyle()).thenReturn(style); + when(style.isFullyLoaded()).thenReturn(true); } @Test