Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UILongPressGestureRecognizer_Class/Reference/Reference.html">UILongPressGestureRecognizer</a> for this. Wherever you create or initialize the mapview, first attach the recognizer to it:</p> <pre><code>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]; </code></pre> <p>Then in the gesture handler:</p> <pre><code>- (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]; } </code></pre> <p>YourMKAnnotationClass is a class you define that conforms to the <a href="http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotation_Protocol/Reference/Reference.html">MKAnnotation</a> protocol. If your app will only be running on iOS 4.0 or later, you can use the pre-defined <a href="http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKPointAnnotation_class/Reference/Reference.html">MKPointAnnotation</a> class instead.</p> <p>For examples on creating your own MKAnnotation class, see the sample apps <a href="http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html">WeatherMap</a> and <a href="http://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html">MapCallouts</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload