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.
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
}
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)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With