如何在拖动MKMapView时同时拖动注释?(iOS)

6

我有一个包含MKMapView的UIViewController。我通过以下代码在我的当前位置添加了一个注释:

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的提示或好的教程。

enter image description here

enter image description here

3个回答

14

首先,您需要为您的注释返回一个视图。您可以像下面这样做:

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协议,就可以通过以下方式获取拖动标记的坐标:

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
    if newState == MKAnnotationViewDragState.Ending {
        let droppedAt = view.annotation?.coordinate
        print(droppedAt)
    }
}

另请参考我的示例代码

编辑

替代方案:

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)
}
不要忘记在viewForAnnotation中添加pinView?.animatesDrop = true

这里输入图片描述


我发现你的示例代码很有帮助。你的代码实现了一个可拖动的注释标记,需要选择标记才能拖动它。我正在尝试实现自动拖动标记,使其随着地图在被拖动时自动移动到位于地图中心的位置。 - user2768374

1

还有另一种解决方案。在这篇文章中:确定MKMapView是否被拖动/移动,得票最高的答案使用了一个UIPanGestureRecognizer在地图视图上。您可以使用它来在拖动地图时移动标记。


1

这里有一些代码,当用户拖动或缩放地图时,会实时更新注释:

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()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接