Skip to content

Commit

Permalink
fix: onUserTrackingModeChange on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiKilian committed Dec 8, 2024
1 parent 693cfb3 commit 63e2976
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.location.Location;
import android.util.Log;

import org.maplibre.android.camera.CameraPosition;
import org.maplibre.android.camera.CameraUpdate;
Expand All @@ -11,13 +12,9 @@
import org.maplibre.android.geometry.VisibleRegion;
import org.maplibre.android.location.OnCameraTrackingChangedListener;
import org.maplibre.android.location.modes.CameraMode;
import org.maplibre.android.location.modes.RenderMode;
import org.maplibre.android.maps.MapLibreMap;
import org.maplibre.android.maps.Style;
import org.maplibre.android.location.LocationComponent;
import org.maplibre.android.location.LocationComponentOptions;
import org.maplibre.android.location.LocationComponentActivationOptions;
// import org.maplibre.android.plugins.locationlayer.LocationLayerPlugin;

import org.maplibre.reactnative.components.AbstractMapFeature;
import org.maplibre.reactnative.components.location.LocationComponentManager;
import org.maplibre.reactnative.components.mapview.MLRNMapView;
Expand All @@ -31,8 +28,6 @@
import org.maplibre.reactnative.location.UserTrackingState;
import org.maplibre.reactnative.utils.GeoJSONUtils;

import org.maplibre.reactnative.R;

import org.maplibre.reactnative.events.constants.EventTypes;

import com.facebook.react.bridge.WritableMap;
Expand Down Expand Up @@ -87,12 +82,12 @@ public class MLRNCamera extends AbstractMapFeature {
private LocationManager.OnUserLocationChange mLocationChangeListener = new LocationManager.OnUserLocationChange() {
@Override
public void onLocationChange(Location nextLocation) {
if (getMapboxMap() == null || mLocationComponentManager == null || !mLocationComponentManager.hasLocationComponent() || (!mFollowUserLocation)) {
return;
}
if (getMapboxMap() == null || mLocationComponentManager == null || !mLocationComponentManager.hasLocationComponent() || (!mFollowUserLocation)) {
return;
}

mUserLocation.setCurrentLocation(nextLocation);
sendUserLocationUpdateEvent(nextLocation);
mUserLocation.setCurrentLocation(nextLocation);
sendUserLocationUpdateEvent(nextLocation);
}
};

Expand Down Expand Up @@ -236,12 +231,7 @@ private CameraPosition getUserLocationUpdateCameraPosition(double zoomLevel) {
}
}

return new CameraPosition.Builder()
.target(center)
.bearing(getDirectionForUserLocationUpdate())
.tilt(mPitch)
.zoom(zoomLevel)
.build();
return new CameraPosition.Builder().target(center).bearing(getDirectionForUserLocationUpdate()).tilt(mPitch).zoom(zoomLevel).build();
}

private double getDirectionForUserLocationUpdate() {
Expand All @@ -260,7 +250,7 @@ private double getDirectionForUserLocationUpdate() {
}

private void sendUserLocationUpdateEvent(Location location) {
if(location == null){
if (location == null) {
return;
}
IEvent event = new MapChangeEvent(this, EventTypes.USER_LOCATION_UPDATED, makeLocationChangePayload(location));
Expand Down Expand Up @@ -376,37 +366,38 @@ private void updateLocationLayer(@NonNull Style style) {
}

mLocationComponentManager.update(style);

if (mFollowUserLocation) {
mLocationComponentManager.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
}
mLocationComponentManager.setFollowUserLocation(mFollowUserLocation);

if (mFollowUserLocation) {
mLocationComponentManager.setCameraMode(UserTrackingMode.getCameraMode(mUserTrackingMode));
mLocationComponentManager.addOnCameraTrackingChangedListener(new OnCameraTrackingChangedListener() {
@Override public void onCameraTrackingChanged(int currentMode) {
int userTrackingMode = UserTrackingMode.NONE;
switch (currentMode) {
case CameraMode.NONE:
userTrackingMode = UserTrackingMode.NONE;
break;
case CameraMode.TRACKING:
userTrackingMode = UserTrackingMode.FOLLOW;
break;
case CameraMode.TRACKING_COMPASS:
userTrackingMode = UserTrackingMode.FollowWithHeading;
break;
case CameraMode.TRACKING_GPS:
userTrackingMode = UserTrackingMode.FollowWithCourse;
break;
default:
userTrackingMode = UserTrackingMode.NONE;
}
updateUserTrackingMode(userTrackingMode);
}
@Override public void onCameraTrackingDismissed() {
@Override
public void onCameraTrackingChanged(int currentMode) {
int userTrackingMode;

switch (currentMode) {
case CameraMode.NONE:
userTrackingMode = UserTrackingMode.NONE;
break;
case CameraMode.TRACKING:
userTrackingMode = UserTrackingMode.FOLLOW;
break;
case CameraMode.TRACKING_COMPASS:
userTrackingMode = UserTrackingMode.FollowWithHeading;
break;
case CameraMode.TRACKING_GPS:
userTrackingMode = UserTrackingMode.FollowWithCourse;
break;
default:
userTrackingMode = UserTrackingMode.NONE;
}

updateUserTrackingMode(userTrackingMode);
}

@Override
public void onCameraTrackingDismissed() {
}
});
} else {
mLocationComponentManager.setCameraMode(CameraMode.NONE);
Expand All @@ -429,10 +420,7 @@ public void setZoomLevel(double zoomLevel) {
}

private CameraPosition buildCamera(CameraPosition previousPosition, boolean shouldUpdateTarget) {
CameraPosition.Builder builder = new CameraPosition.Builder(previousPosition)
.bearing(mHeading)
.tilt(mPitch)
.zoom(mZoomLevel);
CameraPosition.Builder builder = new CameraPosition.Builder(previousPosition).bearing(mHeading).tilt(mPitch).zoom(mZoomLevel);

if (shouldUpdateTarget) {
builder.target(GeoJSONUtils.toLatLng(mCenterCoordinate));
Expand Down Expand Up @@ -507,6 +495,7 @@ MapLibreMap getMapboxMap() {
/**
* Create a payload of the location data per the web api geolocation spec
* https://dev.w3.org/geo/api/spec-source.html#position
*
* @return
*/
private WritableMap makeLocationChangePayload(Location location) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public LocationComponentManager(MLRNMapView mlrnMapView, Context context) {

private boolean mShowingUserLocation = false;

private OnCameraTrackingChangedListener mOnCameraTrackingChangedListener = null;

public void showUserLocation(boolean showUserLocation) {
mShowUserLocation = showUserLocation;
stateChanged();
Expand All @@ -68,16 +70,21 @@ public void setRenderMode(@RenderMode.Mode int renderMode) {
}

public void setPreferredFramesPerSecond(int preferredFramesPerSecond) {
if(mLocationComponent == null || preferredFramesPerSecond <= 0) {
if (mLocationComponent == null || preferredFramesPerSecond <= 0) {
return;
}
}

mLocationComponent.setMaxAnimationFps(preferredFramesPerSecond);
}


public void addOnCameraTrackingChangedListener(OnCameraTrackingChangedListener onCameraTrackingChangedListener) {
mLocationComponent.addOnCameraTrackingChangedListener(onCameraTrackingChangedListener);
if (mOnCameraTrackingChangedListener != null) {
mLocationComponent.removeOnCameraTrackingChangedListener(mOnCameraTrackingChangedListener);
}

mOnCameraTrackingChangedListener = onCameraTrackingChangedListener;

mLocationComponent.addOnCameraTrackingChangedListener(mOnCameraTrackingChangedListener);
}

@SuppressLint("MissingPermission")
Expand Down Expand Up @@ -111,7 +118,7 @@ public void update(@NonNull Style style) {
public void update(boolean displayUserLocation, @NonNull Style style) {
Integer tintColor = mMapView.getTintColor();

if (mLocationComponent == null || tintColor != null ) {
if (mLocationComponent == null || tintColor != null) {
mLocationComponent = mMap.getLocationComponent();

LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
Expand Down Expand Up @@ -148,10 +155,10 @@ LocationComponentOptions options(boolean displayUserLocation) {
.accuracyAlpha(0.0f);
} else if (tintColor != null) {
builder = builder
.enableStaleState(false)
.bearingTintColor(tintColor)
.foregroundTintColor(tintColor)
.accuracyColor(tintColor);
.enableStaleState(false)
.bearingTintColor(tintColor)
.foregroundTintColor(tintColor)
.accuracyColor(tintColor);
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.maplibre.reactnative.components.mapview;

import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -11,23 +10,16 @@
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import org.maplibre.android.geometry.LatLngBounds;
import org.maplibre.android.log.Logger;
import org.maplibre.android.maps.MapLibreMap;
import org.maplibre.reactnative.components.AbstractEventEmitter;
import org.maplibre.reactnative.events.constants.EventKeys;
import org.maplibre.reactnative.utils.ConvertUtils;
import org.maplibre.reactnative.utils.ExpressionParser;
import org.maplibre.reactnative.utils.GeoJSONUtils;
import org.maplibre.geojson.FeatureCollection;
import org.maplibre.geojson.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;

import javax.annotation.Nullable;

Expand Down
22 changes: 15 additions & 7 deletions src/components/Camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { point } from "@turf/helpers";
import {
forwardRef,
memo,
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useState,
} from "react";
import { requireNativeComponent, type ViewProps } from "react-native";
import { Platform, requireNativeComponent, type ViewProps } from "react-native";

import { CameraModes } from "../MLRNModule";
import { useNativeRef } from "../hooks/useNativeRef";
Expand Down Expand Up @@ -466,12 +467,19 @@ const Camera = memo(

useEffect(() => {
if (followUserLocation) {
nativeCameraRef.current?.setNativeProps({
...followProps,
});
nativeCameraRef.current?.setNativeProps({
followUserLocation,
});
if (Platform.OS === "android") {
nativeCameraRef.current?.setNativeProps({
...followProps,
followUserLocation,
});
} else {
nativeCameraRef.current?.setNativeProps({
...followProps,
});
nativeCameraRef.current?.setNativeProps({
followUserLocation,
});
}
} else {
nativeCameraRef.current?.setNativeProps({
followUserLocation,
Expand Down

0 comments on commit 63e2976

Please sign in to comment.