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

时间:2022-05-27 08:08:36

I have a UIViewController that contains a MKMapView. I have added an annotation at my current location by the following code:

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

I would like to make the annotation move as the the map view is being moved or dragged by the user. For example my current location is New Albany and if i drag the map not the annotation is will change its stage floating in mid air till i release on top of Pontotoc so the annotation points where i released. I would be grateful for any hits or good tutorials on MapKit.

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

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

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

4 个解决方案

#1


9  

First, you have to return a view for your annotation. You can do so like this:

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

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
}

Then, once you have adopted the MKMapViewDelegate protocol in your controller, you grab the coordinates of a pin after dragging it like this:

然后,一旦你在控制器中采用了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)
    }
}

See also my sample code.

另请参阅我的示例代码。

EDIT

alternative solution:

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

Don't forget pinView?.animatesDrop = true in viewForAnnotation.

不要忘记viewForAnnotation中的pinView?.animatesDrop = true。

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

#2


1  

There is another solution. In this post: determine if MKMapView was dragged/moved, the answer with the most votes uses a UIPanGestureRecognizer on the map view. You could use this to move the pin AS you drag the map.

还有另一种解决方案。在这篇文章中:确定是否拖动/移动了MKMapView,得票最多的答案在地图视图上使用了UIPanGestureRecognizer。拖动地图时,可以使用此按钮移动引脚。

#3


0  

Here is some code that will update the annotation in near-real-time as the user drags or pinches the map:

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

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

#4


-1  

You can't get there from here.

你不能从这里到达那里。

I'm hoping that this will be fixed in a later iOS release.

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

There seems to be an issue with background updates.

背景更新似乎存在问题。

It's extremely annoying that iOS can't do what JavaScript and AJAX do so easily.

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

#1


9  

First, you have to return a view for your annotation. You can do so like this:

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

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
}

Then, once you have adopted the MKMapViewDelegate protocol in your controller, you grab the coordinates of a pin after dragging it like this:

然后,一旦你在控制器中采用了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)
    }
}

See also my sample code.

另请参阅我的示例代码。

EDIT

alternative solution:

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

Don't forget pinView?.animatesDrop = true in viewForAnnotation.

不要忘记viewForAnnotation中的pinView?.animatesDrop = true。

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

#2


1  

There is another solution. In this post: determine if MKMapView was dragged/moved, the answer with the most votes uses a UIPanGestureRecognizer on the map view. You could use this to move the pin AS you drag the map.

还有另一种解决方案。在这篇文章中:确定是否拖动/移动了MKMapView,得票最多的答案在地图视图上使用了UIPanGestureRecognizer。拖动地图时,可以使用此按钮移动引脚。

#3


0  

Here is some code that will update the annotation in near-real-time as the user drags or pinches the map:

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

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

#4


-1  

You can't get there from here.

你不能从这里到达那里。

I'm hoping that this will be fixed in a later iOS release.

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

There seems to be an issue with background updates.

背景更新似乎存在问题。

It's extremely annoying that iOS can't do what JavaScript and AJAX do so easily.

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