当我们用MapKit开发时,有时候希望在地图拖拽时触发事件,可以用下面的方法。
struct MapView: UIViewRepresentable { func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() mapView.delegate = context.coordinator mapView.showsBuildings = true mapView.showsScale = true mapView.isPitchEnabled = true mapView.showsUserLocation = true let region = MKCoordinateRegion(center: CLLocationCoordinate2DMake(35.292899, 140.085370), span: MKCoordinateSpan(latitudeDelta: 0.75, longitudeDelta: 0.75)) mapView.setRegion(region, animated: true) return mapView } func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) { } func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDelegate { var parent: MapView var mkMapView: MKMapView? init(_ parent: MapView) { self.parent = parent } func mapViewDidFinishLoadingMap(_ mapView: MKMapView) { self.mkMapView = mapView } func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { } // 地图图钉選択場合動作 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { } // ※拖拽地图时触发 func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { // 这里做些自定义的操作,比如重画某些图钉之类的。 drawCenterPoint(mapView) } // ※拖拽地图时触发 func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) { drawCenterPoint(mapView) } // 自定义图钉做成 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard annotation is MKPointAnnotation else { return nil } let identifier = "Annotation" + String(arc4random()) var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) } else { annotationView!.annotation = annotation } let image = UIImage(named: "\(annotation.annotationType)")! annotationView!.image = image.scaleImage(scaleSize: annotation.iconScaleSize) if annotation.annotationType == "Center Point" { let offset = CGPoint(x: 0, y: -(image.size.height / 2)) annotationView!.centerOffset = offset } let nameLbl: UILabel! = UILabel(frame: CGRect(x: -24, y: 40, width: 100, height: 30)) nameLbl.text = "自由文本" nameLbl.textColor = UIColor.black nameLbl.font = UIFont(name: "Atari Classic Extrasmooth", size: 10) nameLbl.textAlignment = NSTextAlignment.center annotationView!.addSubview(nameLbl) annotationView!.displayPriority = .required return annotationView } func drawCenterPoint(_ uiView: MKMapView) { for annotation_loop in uiView.annotations { if annotation_loop is MKUserLocation { continue } if annotation_loop.annotationType == "Center Point" { uiView.removeAnnotation(annotation_loop) break; } } let annotation = MKPointAnnotation() annotation.annotationID = String("001") annotation.annotationType = "Center Point" annotation.iconScaleSize = 0.25 annotation.title = "123" annotation.latitude = String(uiView.region.center.latitude) annotation.longitude = String(uiView.region.center.longitude) annotation.coordinate = CLLocationCoordinate2DMake(uiView.region.center.latitude, uiView.region.center.longitude) uiView.addAnnotation(annotation) } } }