如何拖动MKMapView拖动注释? (IOS)

时间:2015-10-17 16:01:34

标签: ios swift mkmapview mapkit

我有一个包含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)
        }
    }
}  

我想在用户移动或拖动地图视图时进行注释移动。例如我当前的位置是New Albany,如果我拖动地图而不是注释将改变它在半空中浮动的阶段,直到我在Pontotoc顶部释放,因此注释指向我释放的位置。对于MapKit上的任何点击或精彩教程,我将不胜感激。

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:10)

首先,您必须返回注释的视图。你可以这样做:

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

另请参阅我的sample code

修改

替代解决方案:

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

请勿忘记pinView?.animatesDrop = true中的viewForAnnotation

enter image description here

答案 1 :(得分:1)

还有另一种解决方案。在这篇文章中:determine if MKMapView was dragged/moved,得票最多的答案在地图视图中使用了UIPanGestureRecognizer。你拖动地图时可以用它来移动别针。

答案 2 :(得分:0)

以下是一些代码,当用户拖动或捏住地图时,它会近乎实时地更新注释:

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

答案 3 :(得分:-1)

You can't get there from here.

我希望这会在以后的iOS版本中修复。

背景更新似乎存在问题。

iOS无法轻松完成JavaScript和AJAX的操作,这非常令人讨厌。