Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -309,11 +317,8 @@ private void prepareLocationSource() {
locationFeature,
new GeoJsonOptions().withMaxZoom(16)
);
}

private void addLocationSource() {
mapboxMap.addSource(locationSource);
isSourceInitialized = true;
}

private void refreshSource() {
Expand All @@ -326,10 +331,6 @@ private void setLocationPoint(Point locationPoint) {
locationFeature = Feature.fromGeometry(locationPoint, properties);
locationSource.setGeoJson(locationFeature);
}

if (!isSourceInitialized) {
addLocationSource();
}
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnLocationStaleListener> onLocationStaleListeners
= new CopyOnWriteArrayList<>();
Expand Down Expand Up @@ -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.
* <p>
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand All @@ -476,6 +489,8 @@ void onLocationLayerStart() {
}

void onLocationLayerStop() {
isStarted = false;
locationLayer.hide();
staleStateManager.onStop();
compassManager.onStop();
locationLayerAnimator.cancelAllAnimations();
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
Expand Down