Skip to content

Commit 0df068c

Browse files
authored
feat: migrate LocationManager to new Architecture (#1111)
1 parent 1b86087 commit 0df068c

File tree

59 files changed

+1137
-1342
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1137
-1342
lines changed

android/src/main/java/org/maplibre/reactnative/MLRNPackage.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MLRNPackage : BaseReactPackage() {
5555

5656
MLRNOfflineModule.REACT_CLASS -> return MLRNOfflineModule(reactContext)
5757
MLRNSnapshotModule.NAME -> return MLRNSnapshotModule(reactContext)
58-
MLRNLocationModule.REACT_CLASS -> return MLRNLocationModule(reactContext)
58+
MLRNLocationModule.NAME -> return MLRNLocationModule(reactContext)
5959
MLRNLogging.REACT_CLASS -> return MLRNLogging(reactContext)
6060
}
6161

@@ -111,13 +111,13 @@ class MLRNPackage : BaseReactPackage() {
111111
isTurboModule = true
112112
)
113113

114-
moduleInfos[MLRNLocationModule.REACT_CLASS] = ReactModuleInfo(
115-
MLRNLocationModule.REACT_CLASS,
116-
MLRNLocationModule.REACT_CLASS,
114+
moduleInfos[MLRNLocationModule.NAME] = ReactModuleInfo(
115+
MLRNLocationModule.NAME,
116+
MLRNLocationModule.NAME,
117117
canOverrideExistingModule = false,
118118
needsEagerInit = false,
119119
isCxxModule = false,
120-
isTurboModule = false
120+
isTurboModule = true
121121
)
122122

123123
moduleInfos[MLRNLogging.REACT_CLASS] = ReactModuleInfo(

android/src/main/java/org/maplibre/reactnative/components/camera/MLRNCamera.kt

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package org.maplibre.reactnative.components.camera
22

3+
import android.Manifest
34
import android.content.Context
45
import android.location.Location
56
import android.util.AttributeSet
7+
import androidx.annotation.RequiresPermission
68
import com.facebook.react.bridge.Arguments
79
import com.facebook.react.bridge.ReactContext
810
import com.facebook.react.bridge.ReadableMap
911
import com.facebook.react.bridge.WritableMap
10-
import com.facebook.react.bridge.WritableNativeMap
1112
import com.facebook.react.uimanager.UIManagerHelper
13+
import com.facebook.react.uimanager.events.Event
1214
import com.facebook.react.uimanager.events.EventDispatcher
1315
import org.maplibre.android.camera.CameraPosition
1416
import org.maplibre.android.camera.CameraUpdateFactory.newCameraPosition
@@ -22,8 +24,6 @@ import org.maplibre.reactnative.components.AbstractMapFeature
2224
import org.maplibre.reactnative.components.camera.constants.CameraEasing
2325
import org.maplibre.reactnative.components.location.LocationComponentManager
2426
import org.maplibre.reactnative.components.mapview.MLRNMapView
25-
import org.maplibre.reactnative.events.MapChangeEvent
26-
import org.maplibre.reactnative.events.TrackUserLocationChangeEvent
2727
import org.maplibre.reactnative.location.LocationManager
2828
import org.maplibre.reactnative.location.LocationManager.OnUserLocationChange
2929
import org.maplibre.reactnative.location.TrackUserLocationMode
@@ -63,13 +63,12 @@ class MLRNCamera(context: Context) : AbstractMapFeature(
6363

6464

6565
private val locationChangeListener: OnUserLocationChange = object : OnUserLocationChange {
66-
override fun onLocationChange(nextLocation: Location) {
66+
override fun onLocationChange(location: Location) {
6767
if (mapView!!.mapLibreMap == null || locationComponentManager == null || !locationComponentManager!!.hasLocationComponent() || trackUserLocation == TrackUserLocationMode.NONE) {
6868
return
6969
}
7070

71-
userLocation.currentLocation = nextLocation
72-
sendUserLocationUpdateEvent(nextLocation)
71+
userLocation.setCurrentLocation(location);
7372
}
7473
}
7574

@@ -103,6 +102,7 @@ class MLRNCamera(context: Context) : AbstractMapFeature(
103102
}
104103
}
105104

105+
@RequiresPermission(allOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION])
106106
override fun addToMap(mapView: MLRNMapView) {
107107
this@MLRNCamera.mapView = mapView
108108

@@ -192,7 +192,7 @@ class MLRNCamera(context: Context) : AbstractMapFeature(
192192
private fun updateUserTrackingMode(trackUserLocationMode: Int) {
193193
userLocation.trackingMode = trackUserLocationMode
194194

195-
val event = TrackUserLocationChangeEvent(surfaceId, id, trackUserLocationMode)
195+
val event = OnTrackUserLocationChangeEvent(surfaceId, id, trackUserLocationMode)
196196
eventDispatcher?.dispatchEvent(event)
197197
}
198198

@@ -232,17 +232,6 @@ class MLRNCamera(context: Context) : AbstractMapFeature(
232232
return direction
233233
}
234234

235-
private fun sendUserLocationUpdateEvent(location: Location?) {
236-
if (location == null) {
237-
return
238-
}
239-
// TODO: This has to be emitted from the proper Component
240-
val event = MapChangeEvent(
241-
surfaceId, id, "onUpdate", makeLocationChangePayload(location)
242-
)
243-
eventDispatcher?.dispatchEvent(event)
244-
}
245-
246235
private fun updateUserLocationSignificantly() {
247236
userTrackingState = TrackUserLocationState.BEGAN
248237

@@ -282,12 +271,13 @@ class MLRNCamera(context: Context) : AbstractMapFeature(
282271
mapView!!.moveCamera(cameraUpdate, callback)
283272
}
284273

274+
@RequiresPermission(allOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION])
285275
private fun enableLocation() {
286276
if (!PermissionsManager.areLocationPermissionsGranted(context)) {
287277
return
288278
}
289279

290-
if (!locationManager.isActive) {
280+
if (!locationManager.isActive()) {
291281
locationManager.enable()
292282
}
293283

@@ -385,25 +375,23 @@ class MLRNCamera(context: Context) : AbstractMapFeature(
385375
return mapView!!.mapLibreMap
386376
}
387377

388-
// TODO: Update structure
389-
private fun makeLocationChangePayload(location: Location): WritableMap {
390-
val positionProperties: WritableMap = WritableNativeMap()
391-
val coordinates: WritableMap = WritableNativeMap()
392-
393-
coordinates.putDouble("longitude", location.longitude)
394-
coordinates.putDouble("latitude", location.latitude)
395-
coordinates.putDouble("altitude", location.altitude)
396-
coordinates.putDouble("accuracy", location.accuracy.toDouble())
397-
// TODO
398-
// A better solution will be to pull the heading from the compass engine,
399-
// unfortunately the api is not public
400-
coordinates.putDouble("heading", location.bearing.toDouble())
401-
coordinates.putDouble("course", location.bearing.toDouble())
402-
coordinates.putDouble("speed", location.speed.toDouble())
403-
404-
positionProperties.putMap("coords", coordinates)
405-
positionProperties.putDouble("timestamp", location.time.toDouble())
406-
407-
return positionProperties
378+
379+
inner class OnTrackUserLocationChangeEvent(
380+
surfaceId: Int,
381+
viewId: Int,
382+
private val trackUserLocationMode: Int
383+
) :
384+
Event<OnTrackUserLocationChangeEvent>(surfaceId, viewId) {
385+
override fun getEventName() = "onTrackUserLocationChange"
386+
387+
override fun getEventData(): WritableMap {
388+
return Arguments.createMap().apply {
389+
putString(
390+
"trackUserLocation",
391+
TrackUserLocationMode.toString(trackUserLocationMode)
392+
)
393+
}
394+
}
408395
}
396+
409397
}

android/src/main/java/org/maplibre/reactnative/events/LocationEvent.java

Lines changed: 0 additions & 100 deletions
This file was deleted.

android/src/main/java/org/maplibre/reactnative/events/TrackUserLocationChangeEvent.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

android/src/main/java/org/maplibre/reactnative/events/constants/EventKeys.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ public class EventKeys {
66
// map events
77
public static final String MAP_CLICK = ns("map.press");
88
public static final String MAP_LONG_CLICK = ns("map.longpress");
9-
public static final String MAP_ONCHANGE = ns("map.change");
10-
public static final String MAP_ON_LOCATION_CHANGE = ns("map.location.change");
119
public static final String MAP_ANDROID_CALLBACK = ns("map.androidcallback");
1210

1311
// point annotation events
@@ -25,8 +23,6 @@ public class EventKeys {
2523
// images event
2624
public static final String IMAGES_MISSING = ns("images.missing");
2725

28-
// location events
29-
public static final String USER_LOCATION_UPDATE = ns("user.location.update");
3026

3127
private static String ns(String name) {
3228
return String.format("%s.%s", NAMESPACE, name);

android/src/main/java/org/maplibre/reactnative/events/constants/EventTypes.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ public class EventTypes {
44
// map event types
55
public static final String MAP_CLICK = "press";
66
public static final String MAP_LONG_CLICK = "longpress";
7-
public static final String USER_LOCATION_UPDATED = "userlocationdupdated";
87

98
// point annotation event types
109
public static final String ANNOTATION_SELECTED = "annotationselected";

0 commit comments

Comments
 (0)