How to add a push pin to a MKMapView(IOS) when touching?

IphoneIosMkmapviewPushpin

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

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)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristophe DeboveView Question on Stackoverflow
Solution 1 - Iphoneuser467105View Answer on Stackoverflow
Solution 2 - IphoneAllan SpreysView Answer on Stackoverflow