I had to get the coordinate of a point where the user touch on a MKMapView. I'm not working with the Interface Builder. Can you give me one example?
Asked
Active
Viewed 4.4k times
2 Answers
196
You can use a UILongPressGestureRecognizer for this. Wherever you create or initialize the mapview, first attach the recognizer to it:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //user needs to press for 2 seconds
[self.mapView addGestureRecognizer:lpgr];
[lpgr release];
Then in the gesture handler:
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
YourMKAnnotationClass *annot = [[YourMKAnnotationClass alloc] init];
annot.coordinate = touchMapCoordinate;
[self.mapView addAnnotation:annot];
[annot release];
}
YourMKAnnotationClass is a class you define that conforms to the MKAnnotation protocol. If your app will only be running on iOS 4.0 or later, you can use the pre-defined MKPointAnnotation class instead.
For examples on creating your own MKAnnotation class, see the sample app MapCallouts.

Cœur
- 37,241
- 25
- 195
- 267
-
7Awesome answer, thanks. Personally I flipped the if-statement to a `==` so it returns if it *isn't* `UIGestureRecognizerStateBegan`. Doing this will drop the pin after the specified time, even if the user is still holding the map which was desirable for me (and how the official Maps app does it). – William Denniss May 07 '11 at 07:42
-
I would just like to say I implemented your answer into my project and it worked like a charm. Thank you for your most excellent answer. – DoubleDunk Jul 04 '11 at 05:31
-
This woks perfect but only in the simmulator for me. No callback on the physical phone. Any ideas? I'm running iOS5 with ARC. – rjgonzo Feb 07 '12 at 06:54
-
@rjgonzo: Should work fine with iOS5, ARC, and device. Try deleting app from device and doing Clean, Rebuild, and re-install. While running on device, add breakpoints or NSLogs to make sure mapView is not nil when adding lpgr. – Feb 07 '12 at 13:31
-
Is it possible to do this animated? – Oliver Ni May 25 '15 at 19:14
-
`release`... The memories! – Nicolas Miari Jan 24 '17 at 05:46
34
Thanks to Anna for providing such a great answer! Here is a Swift version if anybody is interested (the answer has been updated to Swift 4.1 syntax).
Creating UILongPressGestureRecognizer:
let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: #selector(MapViewController.handleLongPress(_:)))
longPressRecogniser.minimumPressDuration = 1.0
mapView.addGestureRecognizer(longPressRecogniser)
Handling the gesture:
@objc func handleLongPress(_ gestureRecognizer : UIGestureRecognizer){
if gestureRecognizer.state != .began { return }
let touchPoint = gestureRecognizer.location(in: mapView)
let touchMapCoordinate = mapView.convert(touchPoint, toCoordinateFrom: mapView)
let album = Album(coordinate: touchMapCoordinate, context: sharedContext)
mapView.addAnnotation(album)
}

Bhavesh Bansal
- 101
- 1
- 8

Allan Spreys
- 5,287
- 5
- 39
- 44
-
1
-
2It is possible to use _**let longPressRecogniser = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")**_ – Dx_ Oct 07 '15 at 05:13
-
@Dx_ yes you can because the recognizer is not being modified. The properties in the recognizer are being modified. – 3366784 Aug 07 '16 at 18:02
-
I get error messages in Swift 3. The error is: "Use of unresolved identifier 'gestureRecogniser' Anyone got a solution? – PhilipS Mar 04 '17 at 09:17
-
Hi @PhilipS, I've updated my answer to Swift 3.0 syntax. I hope it will help. – Allan Spreys Mar 04 '17 at 10:26
-
I get error message in swift 4.2 this error is : "Use of unresolved identifier 'Album'" – PPPPPPPPP Sep 26 '18 at 08:38