Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Expose indicator position change listener #515

Merged
merged 1 commit into from
Aug 4, 2020
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 @@ -20,6 +20,7 @@
import com.mapbox.android.core.location.LocationEngineRequest;
import com.mapbox.android.core.location.LocationEngineResult;
import com.mapbox.android.core.permissions.PermissionsManager;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdate;
Expand Down Expand Up @@ -183,6 +184,8 @@ public final class LocationComponent {
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<OnRenderModeChangedListener> onRenderModeChangedListeners
= new CopyOnWriteArrayList<>();
private final CopyOnWriteArrayList<OnIndicatorPositionChangedListener> onIndicatorPositionChangedListener
= new CopyOnWriteArrayList<>();

// Workaround for too frequent updates, see https://github.com/mapbox/mapbox-gl-native/issues/13587
private long fastestInterval;
Expand Down Expand Up @@ -1274,6 +1277,24 @@ public void removeOnLocationStaleListener(@NonNull OnLocationStaleListener liste
onLocationStaleListeners.remove(listener);
}

/**
* Adds a listener that gets invoked when indicator position changes.
*
* @param listener Listener that gets invoked when indicator position changes
*/
public void addOnIndicatorPositionChangedListener(@NonNull OnIndicatorPositionChangedListener listener) {
onIndicatorPositionChangedListener.add(listener);
}

/**
* Removes a listener that gets invoked when indicator position changes.
*
* @param listener Listener that gets invoked when indicator position changes.
*/
public void removeOnIndicatorPositionChangedListener(@NonNull OnIndicatorPositionChangedListener listener) {
onIndicatorPositionChangedListener.remove(listener);
}

/**
* Internal use.
*/
Expand Down Expand Up @@ -1401,7 +1422,8 @@ private void initialize(@NonNull final Context context, @NonNull Style style, bo
LayerFeatureProvider featureProvider = new LayerFeatureProvider();
LayerBitmapProvider bitmapProvider = new LayerBitmapProvider(context);
locationLayerController = new LocationLayerController(mapboxMap, style, sourceProvider, featureProvider,
bitmapProvider, options, renderModeChangedListener, useSpecializedLocationLayer);
bitmapProvider, options, renderModeChangedListener, indicatorPositionChangedListener,
useSpecializedLocationLayer);
locationCameraController = new LocationCameraController(
context, mapboxMap, transform, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);

Expand Down Expand Up @@ -1775,6 +1797,16 @@ public void onRenderModeChanged(int currentMode) {
}
};

@NonNull
@VisibleForTesting
OnIndicatorPositionChangedListener indicatorPositionChangedListener = new OnIndicatorPositionChangedListener() {
@Override public void onIndicatorPositionChanged(@NonNull Point point) {
for (OnIndicatorPositionChangedListener listener : onIndicatorPositionChangedListener) {
listener.onIndicatorPositionChanged(point);
}
}
};

@NonNull
private final MapboxMap.OnDeveloperAnimationListener developerAnimationListener =
new MapboxMap.OnDeveloperAnimationListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.annotation.Nullable;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.modes.RenderMode;
import com.mapbox.mapboxsdk.log.Logger;
Expand Down Expand Up @@ -42,6 +43,7 @@ final class LocationLayerController {
private final LayerBitmapProvider bitmapProvider;
private LocationComponentOptions options;
private final OnRenderModeChangedListener internalRenderModeChangedListener;
private final OnIndicatorPositionChangedListener internalIndicatorPositionChangedListener;
private final boolean useSpecializedLocationLayer;

private boolean isHidden = true;
Expand All @@ -57,10 +59,12 @@ final class LocationLayerController {
LayerBitmapProvider bitmapProvider,
@NonNull LocationComponentOptions options,
@NonNull OnRenderModeChangedListener internalRenderModeChangedListener,
@NonNull OnIndicatorPositionChangedListener internalIndicatorPositionChangedListener,
boolean useSpecializedLocationLayer) {
this.mapboxMap = mapboxMap;
this.bitmapProvider = bitmapProvider;
this.internalRenderModeChangedListener = internalRenderModeChangedListener;
this.internalIndicatorPositionChangedListener = internalIndicatorPositionChangedListener;
this.useSpecializedLocationLayer = useSpecializedLocationLayer;
this.isStale = options.enableStaleState();
if (useSpecializedLocationLayer) {
Expand Down Expand Up @@ -262,6 +266,8 @@ boolean onMapClick(@NonNull LatLng point) {
@Override
public void onNewAnimationValue(LatLng value) {
locationLayerRenderer.setLatLng(value);
internalIndicatorPositionChangedListener.onIndicatorPositionChanged(
Point.fromLngLat(value.getLongitude(), value.getLatitude(), value.getAltitude()));
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mapbox.mapboxsdk.location;

import androidx.annotation.NonNull;
import com.mapbox.geojson.Point;

/**
* Listener that gets invoked when indicator position changes.
*/
public interface OnIndicatorPositionChangedListener {
/**
* This method is called on each position change of the location indicator, including each animation frame.
*
* @param point indicator's position
*/
void onIndicatorPositionChanged(@NonNull Point point);
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,19 @@ class LocationComponentTest {
verify(renderChangeListener).onRenderModeChanged(RenderMode.NORMAL)
}

@Test
fun internal_indicatorPositionChangedListener_onIndicatorPositionChanged() {
locationComponent.activateLocationComponent(context, mock(Style::class.java), locationEngine, locationEngineRequest, locationComponentOptions)
locationComponent.isLocationComponentEnabled = true

val onIndicatorPositionChangedListener: OnIndicatorPositionChangedListener = mock(OnIndicatorPositionChangedListener::class.java)
locationComponent.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)

locationComponent.indicatorPositionChangedListener.onIndicatorPositionChanged(any())

verify(onIndicatorPositionChangedListener).onIndicatorPositionChanged(any())
}

@Test
fun change_to_gps_mode_symbolLayerBearingValue() {
val location = Location("test")
Expand Down
Loading