diff --git a/ios/RNMBX/RNMBXMapView.swift b/ios/RNMBX/RNMBXMapView.swift index 66ed32669..df67bc74c 100644 --- a/ios/RNMBX/RNMBXMapView.swift +++ b/ios/RNMBX/RNMBXMapView.swift @@ -1398,6 +1398,12 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate { return rnmbxPointAnnotation } } + #if RNMBX_11 + // see https://github.com/rnmapbox/maps/issues/3121 + if let rnmbxPointAnnotation = annotations.object(forKey: annotation.id as NSString) { + return rnmbxPointAnnotation; + } + #endif return nil } @@ -1474,41 +1480,36 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate { for annotation in annotations { if let pointAnnotation = annotation as? PointAnnotation, - let userInfo = pointAnnotation.userInfo { - - if let RNMBXPointAnnotation = userInfo[RNMBXPointAnnotation.key] as? WeakRef { - if let pt = RNMBXPointAnnotation.object { + let pt = lookup(pointAnnotation) { let position = pt.superview?.convert(pt.layer.position, to: nil) var geojson = Feature(geometry: .point(Point(targetPoint))) - geojson.identifier = .string(pt.id) - geojson.properties = [ - "screenPointX": .number(Double(position!.x)), - "screenPointY": .number(Double(position!.y)) - ] - let event = RNMBXEvent(type:.longPress, payload: logged("doHandleLongPress") { try geojson.toJSON() }) - switch (dragState) { - case .began: - guard let onDragStart = pt.onDragStart else { - return - } - onDragStart(event.toJSON()) - case .changed: - guard let onDrag = pt.onDrag else { - return - } - onDrag(event.toJSON()) + geojson.identifier = .string(pt.id) + geojson.properties = [ + "screenPointX": .number(Double(position!.x)), + "screenPointY": .number(Double(position!.y)) + ] + let event = RNMBXEvent(type:.longPress, payload: logged("doHandleLongPress") { try geojson.toJSON() }) + switch (dragState) { + case .began: + guard let onDragStart = pt.onDragStart else { return - case .ended: - guard let onDragEnd = pt.onDragEnd else { - return - } - onDragEnd(event.toJSON()) + } + onDragStart(event.toJSON()) + case .changed: + guard let onDrag = pt.onDrag else { return - default: + } + onDrag(event.toJSON()) + return + case .ended: + guard let onDragEnd = pt.onDragEnd else { return } + onDragEnd(event.toJSON()) + return + default: + return } - } } } } @@ -1543,7 +1544,7 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate { // Find if any `queriedFeatureIds` match an annotation's `id` let draggedAnnotations = self.manager.annotations.filter { queriedFeatureIds.contains($0.id) } - let enabledAnnotations = draggedAnnotations.filter { ($0.userInfo?[RNMBXPointAnnotation.key] as? WeakRef)?.object?.draggable ?? false } + let enabledAnnotations = draggedAnnotations.filter { self.lookup($0)?.draggable ?? false } // If `tappedAnnotations` is not empty, call delegate if !enabledAnnotations.isEmpty { self.draggedAnnotation = enabledAnnotations.first! @@ -1586,9 +1587,19 @@ class RNMBXPointAnnotationManager : AnnotationInteractionDelegate { manager.annotations.removeAll(where: {$0.id == annotation.id}) } - func add(_ annotation: PointAnnotation) { + #if RNMBX_11 + var annotations = NSMapTable.init( + keyOptions: .copyIn, + valueOptions: .weakMemory + ) + #endif + + func add(_ annotation: PointAnnotation, _ rnmbxPointAnnotation: RNMBXPointAnnotation) { manager.annotations.append(annotation) manager.refresh() + #if RNMBX_11 + annotations.setObject(rnmbxPointAnnotation, forKey: annotation.id as NSString) + #endif } func update(_ annotation: PointAnnotation) { diff --git a/ios/RNMBX/RNMBXPointAnnotation.swift b/ios/RNMBX/RNMBXPointAnnotation.swift index 9c8c64d24..9513a97d8 100644 --- a/ios/RNMBX/RNMBXPointAnnotation.swift +++ b/ios/RNMBX/RNMBXPointAnnotation.swift @@ -285,7 +285,7 @@ extension RNMBXPointAnnotation { && annotation.point.coordinates.isValid() && (logged("PointAnnotation: missing id attribute") { return id }) != nil, let pointAnnotationManager = map?.pointAnnotationManager { - pointAnnotationManager.add(annotation) + pointAnnotationManager.add(annotation, self) added = true return true }