Skip to content

Commit

Permalink
fix(ios,v10): centerCoordinate should be converted to feature when pa…
Browse files Browse the repository at this point in the history
…ssing to natie layer (rnmapbox#2094)
  • Loading branch information
mfazekas authored Aug 5, 2022
1 parent 120c375 commit 3ca889e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
38 changes: 24 additions & 14 deletions ios/RCTMGL-v10/RCTMGLCamera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ class RCTMGLCamera : RCTMGLMapComponentBase, LocationConsumer {

if let stops = stop["stops"] as? [[String:Any]] {
stops.forEach {
cameraUpdateQueue.enqueue(stop: toUpdateItem(stop: $0))
if let stop = toUpdateItem(stop: $0) {
cameraUpdateQueue.enqueue(stop: stop)
}
}
} else {
cameraUpdateQueue.enqueue(stop: toUpdateItem(stop: stop))
if let stop = toUpdateItem(stop: stop) {
cameraUpdateQueue.enqueue(stop: stop)
}
}

if let map = map {
Expand All @@ -182,7 +186,7 @@ class RCTMGLCamera : RCTMGLMapComponentBase, LocationConsumer {
}
}

private func toUpdateItem(stop: [String: Any]) -> CameraUpdateItem {
private func toUpdateItem(stop: [String: Any]) -> CameraUpdateItem? {
var zoom: CGFloat?
if let z = stop["zoom"] as? Double {
zoom = CGFloat(z)
Expand All @@ -207,34 +211,39 @@ class RCTMGLCamera : RCTMGLMapComponentBase, LocationConsumer {

var center: LocationCoordinate2D?
if let feature: String = stop["centerCoordinate"] as? String {
let centerFeature : Turf.Feature? = try!

let centerFeature : Turf.Feature? = logged("RCTMGLCamera.toUpdateItem.decode.cc") { try
JSONDecoder().decode(Turf.Feature.self, from: feature.data(using: .utf8)!)

}

switch centerFeature?.geometry {
case .point(let centerPoint):
center = centerPoint.coordinates
default:
fatalError("Unexpected geometry: \(String(describing: centerFeature?.geometry))")
Logger.log(level: .error, message: "RCTMGLCamera.toUpdateItem: Unexpected geometry: \(String(describing: centerFeature?.geometry))")
return nil
}
} else if let feature: String = stop["bounds"] as? String {
let collection : Turf.FeatureCollection? = try!
JSONDecoder().decode(Turf.FeatureCollection.self, from: feature.data(using: .utf8)!)
let collection : Turf.FeatureCollection? = logged("RCTMGLCamera.toUpdateItem.decode.bound") { try
JSONDecoder().decode(Turf.FeatureCollection.self, from: feature.data(using: .utf8)!) }
let features = collection?.features

let ne: CLLocationCoordinate2D
switch features?.first?.geometry {
case .point(let point):
ne = point.coordinates
default:
fatalError("Unexpected geometry: \(String(describing: features?.first?.geometry))")
Logger.log(level: .error, message: "RCTMGLCamera.toUpdateItem: Unexpected geometry: \(String(describing: features?.first?.geometry))")
return nil
}

let sw: CLLocationCoordinate2D
switch features?.last?.geometry {
case .point(let point):
sw = point.coordinates
default:
fatalError("Unexpected geometry: \(String(describing: features?.last?.geometry))")
Logger.log(level: .error, message: "RCTMGLCamera.toUpdateItem: Unexpected geometry: \(String(describing: features?.last?.geometry))")
return nil
}

withMapView { map in
Expand Down Expand Up @@ -305,10 +314,11 @@ class RCTMGLCamera : RCTMGLMapComponentBase, LocationConsumer {
return
}

var updateItem = toUpdateItem(stop: stop)
updateItem.mode = .none
updateItem.duration = 0
updateItem.execute(map: map, cameraAnimator: &cameraAnimator)
if var updateItem = toUpdateItem(stop: stop) {
updateItem.mode = .none
updateItem.duration = 0
updateItem.execute(map: map, cameraAnimator: &cameraAnimator)
}
}

func initialLayout() {
Expand Down
4 changes: 3 additions & 1 deletion javascript/components/Camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ export const Camera = memo(
return null;
}
const _defaultStop: NativeCameraStop = {
centerCoordinate: JSON.stringify(defaultSettings.centerCoordinate),
centerCoordinate: JSON.stringify(
makePoint(defaultSettings.centerCoordinate),
),
bounds: JSON.stringify(defaultSettings.bounds),
heading: defaultSettings.heading ?? 0,
pitch: defaultSettings.pitch ?? 0,
Expand Down

0 comments on commit 3ca889e

Please sign in to comment.