Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a draggable MKPointAnnotation using Swift (IOS)?

Tags:

ios

swift

I'm noob in ios and I need implement a draggable MKPointAnnotation in a MKMapView using swift. I need an example. I now implement the MKMapView and add a MKPointAnnotation in the map (addAnnotation).

My english is bad, help me please!

I found this link,but I didn't understand.

like image 922
Charl Lopez Egusquiza Avatar asked Oct 07 '14 04:10

Charl Lopez Egusquiza


2 Answers

You have to create a custom class, derived from MKMapView. This class has to implement the MKMapViewDelegate protocol.

Then you need 2 steps: Create the annotation object and create a view for that 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 the 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
}
like image 177
zisoft Avatar answered Nov 07 '22 11:11

zisoft


this is my example code. This code allows you to long press on the map to add a Point and to drag that point until you release your finger from the screen. Also take a look to the gestureRecognizer that you have to add on the map view. Hope this could 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)")

}
like image 29
Pablo Avatar answered Nov 07 '22 12:11

Pablo