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

Commit 47df8cf

Browse files
JunDaiJUN DAI
authored andcommitted
Expose indicator position change listener
1 parent 9fd6a2a commit 47df8cf

File tree

5 files changed

+163
-32
lines changed

5 files changed

+163
-32
lines changed

MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationComponent.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.mapbox.android.core.location.LocationEngineRequest;
2121
import com.mapbox.android.core.location.LocationEngineResult;
2222
import com.mapbox.android.core.permissions.PermissionsManager;
23+
import com.mapbox.geojson.Point;
2324
import com.mapbox.mapboxsdk.R;
2425
import com.mapbox.mapboxsdk.camera.CameraPosition;
2526
import com.mapbox.mapboxsdk.camera.CameraUpdate;
@@ -183,6 +184,8 @@ public final class LocationComponent {
183184
= new CopyOnWriteArrayList<>();
184185
private final CopyOnWriteArrayList<OnRenderModeChangedListener> onRenderModeChangedListeners
185186
= new CopyOnWriteArrayList<>();
187+
private final CopyOnWriteArrayList<OnIndicatorPositionChangedListener> onIndicatorPositionChangedListener
188+
= new CopyOnWriteArrayList<>();
186189

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

1280+
/**
1281+
* Adds a listener that gets invoked when indicator position changes.
1282+
*
1283+
* @param listener Listener that gets invoked when indicator position changes
1284+
*/
1285+
public void addOnIndicatorPositionChangedListener(@NonNull OnIndicatorPositionChangedListener listener) {
1286+
onIndicatorPositionChangedListener.add(listener);
1287+
}
1288+
1289+
/**
1290+
* Removes a listener that gets invoked when indicator position changes.
1291+
*
1292+
* @param listener Listener that gets invoked when indicator position changes.
1293+
*/
1294+
public void removeOnIndicatorPositionChangedListener(@NonNull OnIndicatorPositionChangedListener listener) {
1295+
onIndicatorPositionChangedListener.remove(listener);
1296+
}
1297+
12771298
/**
12781299
* Internal use.
12791300
*/
@@ -1401,7 +1422,8 @@ private void initialize(@NonNull final Context context, @NonNull Style style, bo
14011422
LayerFeatureProvider featureProvider = new LayerFeatureProvider();
14021423
LayerBitmapProvider bitmapProvider = new LayerBitmapProvider(context);
14031424
locationLayerController = new LocationLayerController(mapboxMap, style, sourceProvider, featureProvider,
1404-
bitmapProvider, options, renderModeChangedListener, useSpecializedLocationLayer);
1425+
bitmapProvider, options, renderModeChangedListener, indicatorPositionChangedListener,
1426+
useSpecializedLocationLayer);
14051427
locationCameraController = new LocationCameraController(
14061428
context, mapboxMap, transform, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);
14071429

@@ -1775,6 +1797,16 @@ public void onRenderModeChanged(int currentMode) {
17751797
}
17761798
};
17771799

1800+
@NonNull
1801+
@VisibleForTesting
1802+
OnIndicatorPositionChangedListener indicatorPositionChangedListener = new OnIndicatorPositionChangedListener() {
1803+
@Override public void onIndicatorPositionChanged(@NonNull Point point) {
1804+
for (OnIndicatorPositionChangedListener listener : onIndicatorPositionChangedListener) {
1805+
listener.onIndicatorPositionChanged(point);
1806+
}
1807+
}
1808+
};
1809+
17781810
@NonNull
17791811
private final MapboxMap.OnDeveloperAnimationListener developerAnimationListener =
17801812
new MapboxMap.OnDeveloperAnimationListener() {

MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationLayerController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import androidx.annotation.Nullable;
88

99
import com.mapbox.geojson.Feature;
10+
import com.mapbox.geojson.Point;
1011
import com.mapbox.mapboxsdk.geometry.LatLng;
1112
import com.mapbox.mapboxsdk.location.modes.RenderMode;
1213
import com.mapbox.mapboxsdk.log.Logger;
@@ -42,6 +43,7 @@ final class LocationLayerController {
4243
private final LayerBitmapProvider bitmapProvider;
4344
private LocationComponentOptions options;
4445
private final OnRenderModeChangedListener internalRenderModeChangedListener;
46+
private final OnIndicatorPositionChangedListener internalIndicatorPositionChangedListener;
4547
private final boolean useSpecializedLocationLayer;
4648

4749
private boolean isHidden = true;
@@ -57,10 +59,12 @@ final class LocationLayerController {
5759
LayerBitmapProvider bitmapProvider,
5860
@NonNull LocationComponentOptions options,
5961
@NonNull OnRenderModeChangedListener internalRenderModeChangedListener,
62+
@NonNull OnIndicatorPositionChangedListener internalIndicatorPositionChangedListener,
6063
boolean useSpecializedLocationLayer) {
6164
this.mapboxMap = mapboxMap;
6265
this.bitmapProvider = bitmapProvider;
6366
this.internalRenderModeChangedListener = internalRenderModeChangedListener;
67+
this.internalIndicatorPositionChangedListener = internalIndicatorPositionChangedListener;
6468
this.useSpecializedLocationLayer = useSpecializedLocationLayer;
6569
this.isStale = options.enableStaleState();
6670
if (useSpecializedLocationLayer) {
@@ -262,6 +266,8 @@ boolean onMapClick(@NonNull LatLng point) {
262266
@Override
263267
public void onNewAnimationValue(LatLng value) {
264268
locationLayerRenderer.setLatLng(value);
269+
internalIndicatorPositionChangedListener.onIndicatorPositionChanged(
270+
Point.fromLngLat(value.getLongitude(), value.getLatitude(), value.getAltitude()));
265271
}
266272
};
267273

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.mapbox.mapboxsdk.location;
2+
3+
import androidx.annotation.NonNull;
4+
import com.mapbox.geojson.Point;
5+
6+
/**
7+
* Listener that gets invoked when indicator position changes.
8+
*/
9+
public interface OnIndicatorPositionChangedListener {
10+
/**
11+
* This method is called on each position change of the location indicator, including each animation frame.
12+
*
13+
* @param point indicator's position
14+
*/
15+
void onIndicatorPositionChanged(@NonNull Point point);
16+
}

MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationComponentTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,19 @@ class LocationComponentTest {
469469
verify(renderChangeListener).onRenderModeChanged(RenderMode.NORMAL)
470470
}
471471

472+
@Test
473+
fun internal_indicatorPositionChangedListener_onIndicatorPositionChanged() {
474+
locationComponent.activateLocationComponent(context, mock(Style::class.java), locationEngine, locationEngineRequest, locationComponentOptions)
475+
locationComponent.isLocationComponentEnabled = true
476+
477+
val onIndicatorPositionChangedListener: OnIndicatorPositionChangedListener = mock(OnIndicatorPositionChangedListener::class.java)
478+
locationComponent.addOnIndicatorPositionChangedListener(onIndicatorPositionChangedListener)
479+
480+
locationComponent.indicatorPositionChangedListener.onIndicatorPositionChanged(any())
481+
482+
verify(onIndicatorPositionChangedListener).onIndicatorPositionChanged(any())
483+
}
484+
472485
@Test
473486
fun change_to_gps_mode_symbolLayerBearingValue() {
474487
val location = Location("test")

0 commit comments

Comments
 (0)