How to implement drag-and-drop MKPointAnnotation using Swift (IOS)?

I am a noob in ios and I need to implement draggable MKPointAnnotation in MKMapView using swift. I need an example. Now I implement MKMapView and add a MKPointAnnotation on the map (addAnnotation).

My english is bad, help me!

I found a link but I did not understand.

+6
source share
2 answers

You need to create your own class derived from MKMapView. This class must implement the MKMapViewDelegate protocol.

Then you need to follow 2 steps: create an annotation object and create a view for this annotation.

Create annotation:

Somewhere in your code, depends on your needs:

 let annotation = MKPointAnnotation() annotation.setCoordinate(location) // your location here annotation.title = "My Title" annotation.subtitle = "My Subtitle" self.mapView.addAnnotation(annotation) 

Create annotation view

 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { if annotation is MKPointAnnotation { let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") pinAnnotationView.pinColor = .Purple pinAnnotationView.draggable = true pinAnnotationView.canShowCallout = true pinAnnotationView.animatesDrop = true return pinAnnotationView } return nil } 
+8
source

This is my sample code. This code allows you to long press on the map to add a point and drag that point until you release your finger from the screen. Also take a look at the gestureRecognizer you must add on the map. Hope this helps you.

 class TravelLocationMapController: UIViewController, MKMapViewDelegate { @IBOutlet var mapView: MKMapView! var dragPin: MKPointAnnotation! override func viewDidLoad() { super.viewDidLoad() mapView.delegate = self let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: "addPin:") gestureRecognizer.numberOfTouchesRequired = 1 mapView.addGestureRecognizer(gestureRecognizer) } func addPin(gestureRecognizer:UIGestureRecognizer){ let touchPoint = gestureRecognizer.locationInView(mapView) let newCoordinates = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView) if dragPin != nil { dragPin.coordinate = newCoordinates } if gestureRecognizer.state == UIGestureRecognizerState.Began { dragPin = MKPointAnnotation() dragPin.coordinate = newCoordinates mapView.addAnnotation(dragPin) } else if gestureRecognizer.state == UIGestureRecognizerState.Ended { dragPin = nil } } func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKPointAnnotation { let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") pinAnnotationView.pinTintColor = UIColor.purpleColor() pinAnnotationView.animatesDrop = true return pinAnnotationView } return nil } func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { let lat = view.annotation?.coordinate.latitude let long = view.annotation?.coordinate.longitude print("Clic pin lat \(lat) long \(long)") } 
+4
source

Source: https://habr.com/ru/post/976330/


All Articles