From 89d57baab009d3117c28f07bdb2a9d39c570d8e0 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 29 Aug 2024 14:10:06 -0500 Subject: [PATCH 01/12] Use v11 camera method when available --- ios/RNMBX/RNMBXCamera.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 261245ca3..08fbe489e 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -447,17 +447,33 @@ open class RNMBXCamera : RNMBXMapComponentBase { withMapView { map in #if RNMBX_11 - let bounds = [sw, ne] + do { + let bounds: [CLLocationCoordinate2D] = [sw, ne] + camera = try map.mapboxMap.camera( + for: bounds, + camera: .init(cameraState: .init( + center: .init(), + padding: .zero, + zoom: zoom ?? 0, + bearing: heading ?? map.mapboxMap.cameraState.bearing, + pitch: pitch ?? map.mapboxMap.cameraState.pitch + )), + coordinatesPadding: padding, + maxZoom: nil, + offset: nil + ) + } catch { + Logger.log(level: .error, message: "RNMBXCamera.toUpdateItem: Failed to build camera configuration: \(error)") + } #else let bounds = CoordinateBounds(southwest: sw, northeast: ne) - #endif - camera = map.mapboxMap.camera( for: bounds, padding: padding, bearing: heading ?? map.mapboxMap.cameraState.bearing, pitch: pitch ?? map.mapboxMap.cameraState.pitch ) + #endif } } else { camera = CameraOptions( From c470fa32ca98b781bd26055e5f50795957b00cd3 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 29 Aug 2024 14:22:21 -0500 Subject: [PATCH 02/12] Address deprecations and warnings --- ios/RNMBX/RNMBXCamera.swift | 45 ++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 08fbe489e..0f839a341 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -41,16 +41,24 @@ struct CameraUpdateItem { try center.validate() } - switch mode { - case .flight: - map.mapView.camera.fly(to: camera, duration: duration) - case .ease: - map.mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) - case .linear: - map.mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) - default: - map.mapboxMap.setCamera(to: camera) - } + switch mode { + case .flight: + map.withMapView { mapView in + mapView.camera.fly(to: camera, duration: duration) + } + case .ease: + map.withMapView { mapView in + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) + } + case .linear: + map.withMapView { mapView in + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) + } + default: + map.withMapboxMap { mapboxMap in + mapboxMap.setCamera(to: camera) + } + } } } } @@ -87,8 +95,10 @@ open class RNMBXMapComponentBase : UIView, RNMBXMapComponent { } func withMapView(_ callback: @escaping (_ mapView: MapView) -> Void) { - withRNMBXMapView { mapView in - callback(mapView.mapView) + withRNMBXMapView { map in + map.withMapView { mapView in + callback(mapView) + } } } @@ -544,8 +554,11 @@ open class RNMBXCamera : RNMBXMapComponentBase { return false } - map.mapView.viewport.removeStatusObserver(self) - return super.removeFromMap(map, reason:reason) + withMapView { mapView in + mapView.viewport.removeStatusObserver(self) + } + + return super.removeFromMap(map, reason: reason) } } @@ -584,12 +597,12 @@ extension RNMBXCamera : ViewportStatusObserver { return "compass" case .course: return "course" - case .some(let bearing): + case .some(_): return "constant" case .none: return "normal" } - } else if let state = state as? OverviewViewportState { + } else if let _ = state as? OverviewViewportState { return "overview" } else { return "custom" From 23c237c96bea2aeae2ea144a02be121cf51d7065 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 29 Aug 2024 16:12:45 -0500 Subject: [PATCH 03/12] Fix deprecated camera method --- .../rnmbx/components/camera/CameraStop.kt | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt index bea5d40e2..868a838dd 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt @@ -2,17 +2,19 @@ package com.rnmapbox.rnmbx.components.camera import android.animation.Animator import android.content.Context -import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toPointGeometry -import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLng -import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLngBounds -import com.rnmapbox.rnmbx.utils.LatLngBounds -import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView -import com.mapbox.maps.CameraOptions import com.facebook.react.bridge.ReadableMap import com.mapbox.geojson.FeatureCollection +import com.mapbox.geojson.Point +import com.mapbox.maps.CameraOptions import com.mapbox.maps.EdgeInsets +import com.mapbox.maps.ScreenCoordinate import com.rnmapbox.rnmbx.components.camera.constants.CameraMode +import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView +import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLng +import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLngBounds +import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toPointGeometry import com.rnmapbox.rnmbx.utils.LatLng +import com.rnmapbox.rnmbx.utils.LatLngBounds class CameraStop { private var mBearing: Double? = null @@ -124,14 +126,22 @@ class CameraStop { if (mLatLng != null) { builder.center(mLatLng!!.point) } else if (mBounds != null) { + val coordinates = listOf( + mBounds!!.toBounds().northeast, + mBounds!!.toBounds().southwest + ) val tilt = if (mTilt != null) mTilt!! else currentCamera.pitch val bearing = if (mBearing != null) mBearing!! else currentCamera.bearing - val boundsCamera = map.cameraForCoordinateBounds( - mBounds!!.toBounds(), + val boundsCamera = map.cameraForCoordinates( + coordinates, + CameraOptions.Builder() + .pitch(tilt) + .bearing(bearing) + .build(), cameraPaddingEdgeInsets, - bearing, - tilt + null, + ScreenCoordinate(0.0, 0.0) ) builder.center(boundsCamera.center) builder.anchor(boundsCamera.anchor) From 5823880cae67f7c8ff5ab8d4d18ba879847a23ba Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 12 Sep 2024 13:00:18 -0500 Subject: [PATCH 04/12] Consolidate switch inside of map getter --- ios/RNMBX/RNMBXCamera.swift | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 0f839a341..14d02e129 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -40,25 +40,19 @@ struct CameraUpdateItem { if let center = camera.center { try center.validate() } - + + map.withMapView { mapView in switch mode { case .flight: - map.withMapView { mapView in - mapView.camera.fly(to: camera, duration: duration) - } + mapView.camera.fly(to: camera, duration: duration) case .ease: - map.withMapView { mapView in - mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) - } - case .linear: - map.withMapView { mapView in - mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) - } + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) + case .linear: + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) default: - map.withMapboxMap { mapboxMap in - mapboxMap.setCamera(to: camera) - } + mapView.mapboxMap.setCamera(to: camera) } + } } } } From 5290700a067d8fb7be0f65b9b1840b72f8cad8dc Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Fri, 13 Sep 2024 15:21:59 -0500 Subject: [PATCH 05/12] Get mapView directly --- ios/RNMBX/RNMBXCamera.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 14d02e129..cdd38a96a 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -548,10 +548,7 @@ open class RNMBXCamera : RNMBXMapComponentBase { return false } - withMapView { mapView in - mapView.viewport.removeStatusObserver(self) - } - + map._mapView.viewport.removeStatusObserver(self) return super.removeFromMap(map, reason: reason) } } From 762c1e764e5eff2512bf8d719bfd3368c5f67229 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Mon, 16 Sep 2024 10:31:46 -0500 Subject: [PATCH 06/12] Avoid setting camera with empty config See https://github.com/mapbox/mapbox-maps-android/issues/2454 --- .../com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt index 6b18caa34..faa212637 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt @@ -76,10 +76,11 @@ class CameraUpdateItem( } } val map = mMap.get() - if (map == null) { + if (map == null || mCameraUpdate.center == null) { isCameraActionCancelled = true return } + val animationOptions = MapAnimationOptions.Builder(); // animateCamera / easeCamera only allows positive duration From 42e672674a555267a87029b06377541e5eda8c04 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 29 Aug 2024 14:10:06 -0500 Subject: [PATCH 07/12] Use v11 camera method when available --- ios/RNMBX/RNMBXCamera.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 261245ca3..08fbe489e 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -447,17 +447,33 @@ open class RNMBXCamera : RNMBXMapComponentBase { withMapView { map in #if RNMBX_11 - let bounds = [sw, ne] + do { + let bounds: [CLLocationCoordinate2D] = [sw, ne] + camera = try map.mapboxMap.camera( + for: bounds, + camera: .init(cameraState: .init( + center: .init(), + padding: .zero, + zoom: zoom ?? 0, + bearing: heading ?? map.mapboxMap.cameraState.bearing, + pitch: pitch ?? map.mapboxMap.cameraState.pitch + )), + coordinatesPadding: padding, + maxZoom: nil, + offset: nil + ) + } catch { + Logger.log(level: .error, message: "RNMBXCamera.toUpdateItem: Failed to build camera configuration: \(error)") + } #else let bounds = CoordinateBounds(southwest: sw, northeast: ne) - #endif - camera = map.mapboxMap.camera( for: bounds, padding: padding, bearing: heading ?? map.mapboxMap.cameraState.bearing, pitch: pitch ?? map.mapboxMap.cameraState.pitch ) + #endif } } else { camera = CameraOptions( From 8582e6bd75fcb15d7c425b60f1b29a3b4e35d174 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 29 Aug 2024 14:22:21 -0500 Subject: [PATCH 08/12] Address deprecations and warnings --- ios/RNMBX/RNMBXCamera.swift | 45 ++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 08fbe489e..0f839a341 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -41,16 +41,24 @@ struct CameraUpdateItem { try center.validate() } - switch mode { - case .flight: - map.mapView.camera.fly(to: camera, duration: duration) - case .ease: - map.mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) - case .linear: - map.mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) - default: - map.mapboxMap.setCamera(to: camera) - } + switch mode { + case .flight: + map.withMapView { mapView in + mapView.camera.fly(to: camera, duration: duration) + } + case .ease: + map.withMapView { mapView in + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) + } + case .linear: + map.withMapView { mapView in + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) + } + default: + map.withMapboxMap { mapboxMap in + mapboxMap.setCamera(to: camera) + } + } } } } @@ -87,8 +95,10 @@ open class RNMBXMapComponentBase : UIView, RNMBXMapComponent { } func withMapView(_ callback: @escaping (_ mapView: MapView) -> Void) { - withRNMBXMapView { mapView in - callback(mapView.mapView) + withRNMBXMapView { map in + map.withMapView { mapView in + callback(mapView) + } } } @@ -544,8 +554,11 @@ open class RNMBXCamera : RNMBXMapComponentBase { return false } - map.mapView.viewport.removeStatusObserver(self) - return super.removeFromMap(map, reason:reason) + withMapView { mapView in + mapView.viewport.removeStatusObserver(self) + } + + return super.removeFromMap(map, reason: reason) } } @@ -584,12 +597,12 @@ extension RNMBXCamera : ViewportStatusObserver { return "compass" case .course: return "course" - case .some(let bearing): + case .some(_): return "constant" case .none: return "normal" } - } else if let state = state as? OverviewViewportState { + } else if let _ = state as? OverviewViewportState { return "overview" } else { return "custom" From bc011abfdc57c0656f1f9449879a4f20c1ac7d3c Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 29 Aug 2024 16:12:45 -0500 Subject: [PATCH 09/12] Fix deprecated camera method --- .../rnmbx/components/camera/CameraStop.kt | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt index bea5d40e2..868a838dd 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraStop.kt @@ -2,17 +2,19 @@ package com.rnmapbox.rnmbx.components.camera import android.animation.Animator import android.content.Context -import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toPointGeometry -import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLng -import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLngBounds -import com.rnmapbox.rnmbx.utils.LatLngBounds -import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView -import com.mapbox.maps.CameraOptions import com.facebook.react.bridge.ReadableMap import com.mapbox.geojson.FeatureCollection +import com.mapbox.geojson.Point +import com.mapbox.maps.CameraOptions import com.mapbox.maps.EdgeInsets +import com.mapbox.maps.ScreenCoordinate import com.rnmapbox.rnmbx.components.camera.constants.CameraMode +import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView +import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLng +import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toLatLngBounds +import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toPointGeometry import com.rnmapbox.rnmbx.utils.LatLng +import com.rnmapbox.rnmbx.utils.LatLngBounds class CameraStop { private var mBearing: Double? = null @@ -124,14 +126,22 @@ class CameraStop { if (mLatLng != null) { builder.center(mLatLng!!.point) } else if (mBounds != null) { + val coordinates = listOf( + mBounds!!.toBounds().northeast, + mBounds!!.toBounds().southwest + ) val tilt = if (mTilt != null) mTilt!! else currentCamera.pitch val bearing = if (mBearing != null) mBearing!! else currentCamera.bearing - val boundsCamera = map.cameraForCoordinateBounds( - mBounds!!.toBounds(), + val boundsCamera = map.cameraForCoordinates( + coordinates, + CameraOptions.Builder() + .pitch(tilt) + .bearing(bearing) + .build(), cameraPaddingEdgeInsets, - bearing, - tilt + null, + ScreenCoordinate(0.0, 0.0) ) builder.center(boundsCamera.center) builder.anchor(boundsCamera.anchor) From 715adf720c85761b1ae0652f2ede703ef919931c Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Thu, 12 Sep 2024 13:00:18 -0500 Subject: [PATCH 10/12] Consolidate switch inside of map getter --- ios/RNMBX/RNMBXCamera.swift | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 0f839a341..14d02e129 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -40,25 +40,19 @@ struct CameraUpdateItem { if let center = camera.center { try center.validate() } - + + map.withMapView { mapView in switch mode { case .flight: - map.withMapView { mapView in - mapView.camera.fly(to: camera, duration: duration) - } + mapView.camera.fly(to: camera, duration: duration) case .ease: - map.withMapView { mapView in - mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) - } - case .linear: - map.withMapView { mapView in - mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) - } + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .easeInOut, completion: nil) + case .linear: + mapView.camera.ease(to: camera, duration: duration ?? 0, curve: .linear, completion: nil) default: - map.withMapboxMap { mapboxMap in - mapboxMap.setCamera(to: camera) - } + mapView.mapboxMap.setCamera(to: camera) } + } } } } From 67183ffc6468dd1abeab163923e1b14e51b7497e Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Fri, 13 Sep 2024 15:21:59 -0500 Subject: [PATCH 11/12] Get mapView directly --- ios/RNMBX/RNMBXCamera.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ios/RNMBX/RNMBXCamera.swift b/ios/RNMBX/RNMBXCamera.swift index 14d02e129..cdd38a96a 100644 --- a/ios/RNMBX/RNMBXCamera.swift +++ b/ios/RNMBX/RNMBXCamera.swift @@ -548,10 +548,7 @@ open class RNMBXCamera : RNMBXMapComponentBase { return false } - withMapView { mapView in - mapView.viewport.removeStatusObserver(self) - } - + map._mapView.viewport.removeStatusObserver(self) return super.removeFromMap(map, reason: reason) } } From 2f46a22b2dff197b3d9767824fa354e8abd403a1 Mon Sep 17 00:00:00 2001 From: Naftali Beder Date: Mon, 16 Sep 2024 10:31:46 -0500 Subject: [PATCH 12/12] Avoid setting camera with empty config See https://github.com/mapbox/mapbox-maps-android/issues/2454 --- .../com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt index 6b18caa34..faa212637 100644 --- a/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt +++ b/android/src/main/java/com/rnmapbox/rnmbx/components/camera/CameraUpdateItem.kt @@ -76,10 +76,11 @@ class CameraUpdateItem( } } val map = mMap.get() - if (map == null) { + if (map == null || mCameraUpdate.center == null) { isCameraActionCancelled = true return } + val animationOptions = MapAnimationOptions.Builder(); // animateCamera / easeCamera only allows positive duration