How to drag an annotation with MKMapView being dragged? (iOS)
我有一个包含MKMapView的UIViewController。 我通过以下代码在当前位置添加了注释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import UIKit import MapKit class LocationViewController: UIViewController , MKMapViewDelegate ,CLLocationManagerDelegate { @IBOutlet weak var mapView: MKMapView! var locationManager: CLLocationManager! let regionRadius: CLLocationDistance = 1000 var token: dispatch_once_t = 0 override func viewDidLoad() { super.viewDidLoad() mapView.delegate = self locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } func centerMapOnLocation(location: CLLocation) { let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0) mapView.setRegion(coordinateRegion, animated: true) } func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) { if ((userLocation.coordinate.latitude != 0.0) && (userLocation.coordinate.longitude != 0.0)) { dispatch_once(&token) { self.centerMapOnLocation(userLocation.location!) let annotation = MapPin(title:"This Location", locationName:"Test", discipline:"None", coordinate: userLocation.coordinate) mapView.addAnnotation(annotation) } } } |
我想在用户移动或拖动地图视图时使注释移动。 例如,我当前的位置是"新奥尔巴尼",如果我拖动地图,则注释将不会漂浮在空中,直到我在Pontotoc顶部释放时注释才释放。 我将对MapKit的任何点击量或良好的教程表示感谢。
首先,您必须返回注释的视图。 您可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } let reuseId ="pin" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) pinView?.draggable = true } else { pinView?.annotation = annotation } return pinView } |
然后,一旦在控制器中采用了MKMapViewDelegate协议,就可以像下面这样拖动它来获取销的坐标:
1 2 3 4 5 6 | func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) { if newState == MKAnnotationViewDragState.Ending { let droppedAt = view.annotation?.coordinate print(droppedAt) } } |
另请参阅我的示例代码。
编辑
替代解决方案:
1 2 3 4 5 6 7 8 9 10 11 | func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) { // Remove all annotations self.mapView.removeAnnotations(mapView.annotations) // Add new annotation let annotation = MKPointAnnotation() annotation.coordinate = mapView.centerCoordinate annotation.title ="title" annotation.subtitle ="subtitle" self.mapView.addAnnotation(annotation) } |
不要忘记
以下是一些代码,这些代码将在用户拖动或捏捏地图时几乎实时更新注释:
1 2 3 4 5 6 7 8 9 10 11 12 | var mapRegionTimer: Timer? public func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) { mapRegionTimer?.invalidate() mapRegionTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { (t) in self.myAnnotation.coordinate = CLLocationCoordinate2DMake(mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude); self.myAnnotation.title ="Current location" self.mapView.addAnnotation(self.myAnnotation) }) } public func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) { mapRegionTimer?.invalidate() } |
还有另一种解决方案。 在这篇文章中:确定是否拖动/移动了MKMapView,投票最多的答案在地图视图上使用了UIPanGestureRecognizer。 您可以通过拖动地图来移动图钉。