I'm trying to make an iPhone app which requires users to be able to long press on a place on a map view to drop a pin there. Does anybody know how this is done?
The behaviour is observable in apple maps when you long press on the screen. It will drop a pin and present an annotation saying "dropped pin"
Update Swift3
First declare
UIGestureRecognizer
inviewDidLoad
Second add the function for longPress
I recommend creating the annotations in an array that will serve you later if you want to delete it, like this...
If you have different annotations, you can delete only the annotations you want, for that when you add a new annotation add to the array. To delete only one group of annotations just do the following
To delete all annotations try
Apologies for the translation....
1) Instantiate a
UILongPressGestureRecognizer
and add it to theMKMapView
.2) When the selector gets called after the user has a long press, call the addAnnotation method in
MKMapView
with the appropriate title and coordinate.3) Then make sure you conform to the
MKMapViewDelegate
and implementviewForAnnotation:
which will be called right after you add the annotation and return aMKPinAnnotationView
add
UILongPressGestureRecognizer
to your MapViewAdd annotation on Long press detect - func:
or you can add annotation without any title: