diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java index 6649b8dc9..faf227ea7 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java @@ -93,7 +93,8 @@ final class LocationLayer implements LocationLayerAnimator.OnLayerAnimationsValu Point.fromLngLat(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) ); private GeoJsonSource locationSource; - private boolean isSourceInitialized; + + private boolean isHidden; LocationLayer(MapView mapView, MapboxMap mapboxMap, LocationLayerOptions options) { this.mapboxMap = mapboxMap; @@ -103,10 +104,15 @@ final class LocationLayer implements LocationLayerAnimator.OnLayerAnimationsValu } void initializeComponents(LocationLayerOptions options) { - prepareLocationSource(); addLocationSource(); addLayers(); applyStyle(options); + + if (isHidden) { + hide(); + } else { + show(); + } } void applyStyle(@NonNull LocationLayerOptions options) { @@ -167,12 +173,14 @@ int getRenderMode() { void show() { setRenderMode(renderMode); + isHidden = false; } void hide() { for (String layerId : layerMap.keySet()) { setLayerVisibility(layerId, false); } + isHidden = true; } private void setLayerVisibility(String layerId, boolean visible) { @@ -299,7 +307,7 @@ void updateForegroundBearing(float bearing) { // Source actions // - private void prepareLocationSource() { + private void addLocationSource() { locationFeature.addNumberProperty(PROPERTY_GPS_BEARING, 0f); locationFeature.addNumberProperty(PROPERTY_COMPASS_BEARING, 0f); locationFeature.addBooleanProperty(PROPERTY_LOCATION_STALE, false); @@ -309,11 +317,8 @@ private void prepareLocationSource() { locationFeature, new GeoJsonOptions().withMaxZoom(16) ); - } - private void addLocationSource() { mapboxMap.addSource(locationSource); - isSourceInitialized = true; } private void refreshSource() { @@ -326,10 +331,6 @@ private void setLocationPoint(Point locationPoint) { locationFeature = Feature.fromGeometry(locationPoint, properties); locationSource.setGeoJson(locationFeature); } - - if (!isSourceInitialized) { - addLocationSource(); - } } // diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index e41d6d7a8..cfdbd3c4b 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -71,6 +71,8 @@ public final class LocationLayerPlugin implements LifecycleObserver { private CameraPosition lastCameraPosition; private boolean isEnabled; + private boolean isStarted; + private StaleStateManager staleStateManager; private final CopyOnWriteArrayList onLocationStaleListeners = new CopyOnWriteArrayList<>(); @@ -163,6 +165,16 @@ public void setLocationLayerEnabled(boolean isEnabled) { } } + /** + * Returns whether the plugin is enabled, meaning that location can be displayed and camera modes can be used. + * + * @return true if the plugin is enabled, false otherwise + * @since 0.6.0 + */ + public boolean isLocationLayerEnabled() { + return isEnabled; + } + /** * Sets the camera mode, which determines how the map camera will track the rendered location. *

@@ -441,10 +453,8 @@ public void removeOnLocationStaleListener(@NonNull OnLocationStaleListener liste */ @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onStart() { - enableLocationLayerPlugin(); - if (isEnabled) { - mapView.addOnMapChangedListener(onMapChangedListener); - } + onLocationLayerStart(); + mapView.addOnMapChangedListener(onMapChangedListener); } /** @@ -454,17 +464,20 @@ public void onStart() { */ @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void onStop() { - disableLocationLayerPlugin(); + onLocationLayerStop(); mapView.removeOnMapChangedListener(onMapChangedListener); } void onLocationLayerStart() { + isStarted = true; if (isEnabled) { if (locationEngine != null) { locationEngine.addLocationEngineListener(locationEngineListener); } setLastLocation(); setLastCompassHeading(); + locationLayer.show(); + setCameraMode(locationLayerCamera.getCameraMode()); } if (mapboxMap != null) { mapboxMap.addOnCameraMoveListener(onCameraMoveListener); @@ -476,6 +489,8 @@ void onLocationLayerStart() { } void onLocationLayerStop() { + isStarted = false; + locationLayer.hide(); staleStateManager.onStop(); compassManager.onStop(); locationLayerAnimator.cancelAllAnimations(); @@ -522,13 +537,11 @@ private void initializeLocationEngine() { private void enableLocationLayerPlugin() { isEnabled = true; onLocationLayerStart(); - locationLayer.show(); } private void disableLocationLayerPlugin() { isEnabled = false; onLocationLayerStop(); - locationLayer.hide(); } private void updateMapWithOptions(final LocationLayerOptions options) { @@ -547,7 +560,7 @@ private void updateMapWithOptions(final LocationLayerOptions options) { * @since 0.1.0 */ private void updateLocation(final Location location) { - if (location == null || !isEnabled) { + if (location == null || !isStarted) { return; } staleStateManager.updateLatestLocationTime(); @@ -649,8 +662,6 @@ public void onMapChanged(int change) { } else if (change == MapView.DID_FINISH_LOADING_STYLE) { locationLayer.initializeComponents(options); locationLayerCamera.initializeOptions(options); - setRenderMode(locationLayer.getRenderMode()); - setCameraMode(locationLayerCamera.getCameraMode()); onLocationLayerStart(); } }