Skip to content

Commit b39d0f2

Browse files
authored
fix(v11,ios): work around the userInfo issue in point annotation in v11 (#3261)
1 parent a24a666 commit b39d0f2

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

ios/RNMBX/RNMBXMapView.swift

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,12 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
13981398
return rnmbxPointAnnotation
13991399
}
14001400
}
1401+
#if RNMBX_11
1402+
// see https://github.com/rnmapbox/maps/issues/3121
1403+
if let rnmbxPointAnnotation = annotations.object(forKey: annotation.id as NSString) {
1404+
return rnmbxPointAnnotation;
1405+
}
1406+
#endif
14011407
return nil
14021408
}
14031409

@@ -1474,41 +1480,36 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
14741480

14751481
for annotation in annotations {
14761482
if let pointAnnotation = annotation as? PointAnnotation,
1477-
let userInfo = pointAnnotation.userInfo {
1478-
1479-
if let RNMBXPointAnnotation = userInfo[RNMBXPointAnnotation.key] as? WeakRef<RNMBXPointAnnotation> {
1480-
if let pt = RNMBXPointAnnotation.object {
1483+
let pt = lookup(pointAnnotation) {
14811484
let position = pt.superview?.convert(pt.layer.position, to: nil)
14821485
var geojson = Feature(geometry: .point(Point(targetPoint)))
1483-
geojson.identifier = .string(pt.id)
1484-
geojson.properties = [
1485-
"screenPointX": .number(Double(position!.x)),
1486-
"screenPointY": .number(Double(position!.y))
1487-
]
1488-
let event = RNMBXEvent(type:.longPress, payload: logged("doHandleLongPress") { try geojson.toJSON() })
1489-
switch (dragState) {
1490-
case .began:
1491-
guard let onDragStart = pt.onDragStart else {
1492-
return
1493-
}
1494-
onDragStart(event.toJSON())
1495-
case .changed:
1496-
guard let onDrag = pt.onDrag else {
1497-
return
1498-
}
1499-
onDrag(event.toJSON())
1486+
geojson.identifier = .string(pt.id)
1487+
geojson.properties = [
1488+
"screenPointX": .number(Double(position!.x)),
1489+
"screenPointY": .number(Double(position!.y))
1490+
]
1491+
let event = RNMBXEvent(type:.longPress, payload: logged("doHandleLongPress") { try geojson.toJSON() })
1492+
switch (dragState) {
1493+
case .began:
1494+
guard let onDragStart = pt.onDragStart else {
15001495
return
1501-
case .ended:
1502-
guard let onDragEnd = pt.onDragEnd else {
1503-
return
1504-
}
1505-
onDragEnd(event.toJSON())
1496+
}
1497+
onDragStart(event.toJSON())
1498+
case .changed:
1499+
guard let onDrag = pt.onDrag else {
15061500
return
1507-
default:
1501+
}
1502+
onDrag(event.toJSON())
1503+
return
1504+
case .ended:
1505+
guard let onDragEnd = pt.onDragEnd else {
15081506
return
15091507
}
1508+
onDragEnd(event.toJSON())
1509+
return
1510+
default:
1511+
return
15101512
}
1511-
}
15121513
}
15131514
}
15141515
}
@@ -1543,7 +1544,7 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
15431544

15441545
// Find if any `queriedFeatureIds` match an annotation's `id`
15451546
let draggedAnnotations = self.manager.annotations.filter { queriedFeatureIds.contains($0.id) }
1546-
let enabledAnnotations = draggedAnnotations.filter { ($0.userInfo?[RNMBXPointAnnotation.key] as? WeakRef<RNMBXPointAnnotation>)?.object?.draggable ?? false }
1547+
let enabledAnnotations = draggedAnnotations.filter { self.lookup($0)?.draggable ?? false }
15471548
// If `tappedAnnotations` is not empty, call delegate
15481549
if !enabledAnnotations.isEmpty {
15491550
self.draggedAnnotation = enabledAnnotations.first!
@@ -1586,9 +1587,19 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate {
15861587
manager.annotations.removeAll(where: {$0.id == annotation.id})
15871588
}
15881589

1589-
func add(_ annotation: PointAnnotation) {
1590+
#if RNMBX_11
1591+
var annotations = NSMapTable<NSString, RNMBXPointAnnotation>.init(
1592+
keyOptions: .copyIn,
1593+
valueOptions: .weakMemory
1594+
)
1595+
#endif
1596+
1597+
func add(_ annotation: PointAnnotation, _ rnmbxPointAnnotation: RNMBXPointAnnotation) {
15901598
manager.annotations.append(annotation)
15911599
manager.refresh()
1600+
#if RNMBX_11
1601+
annotations.setObject(rnmbxPointAnnotation, forKey: annotation.id as NSString)
1602+
#endif
15921603
}
15931604

15941605
func update(_ annotation: PointAnnotation) {

ios/RNMBX/RNMBXPointAnnotation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ extension RNMBXPointAnnotation {
285285
&& annotation.point.coordinates.isValid()
286286
&& (logged("PointAnnotation: missing id attribute") { return id }) != nil,
287287
let pointAnnotationManager = map?.pointAnnotationManager {
288-
pointAnnotationManager.add(annotation)
288+
pointAnnotationManager.add(annotation, self)
289289
added = true
290290
return true
291291
}

0 commit comments

Comments
 (0)