1

How do I get the latitude and longitude on the point where the user do a longhold(eg 2s)on the mapView.

I chanced upon a similar question : get coordinates on tap in iphone application

But I don't understand how do I go about handling the touch event on the mapView.

What I intent to achieve it to drop a pin annotation to the location, do nesscary reverse geocoding via Google API and display it on the subtitle. So firstly, I would need to get the required coordinates.

Examples would be very much appreciated.

Thanks.

Community
  • 1
  • 1
Gavin
  • 2,784
  • 6
  • 41
  • 78

1 Answers1

4

You can convert the coordinates of the touch in the view retrieved from the gesture to gps coordinates like this...

- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
        CGPoint touchLocation = [gestureRecognizer locationInView:mapView];

        CLLocationCoordinate2D coordinate;
        coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];
    }
}

Note that in my example, I had the gesture applied to the mapView itself. You would have to pass the view to which you applied the gesture to toCoordinateFromView:mapView

mjisrawi
  • 7,846
  • 3
  • 24
  • 27
  • Awesome. Just had to add the following in viewDidLoad UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)]; longPressGesture.minimumPressDuration = 2; [mapView addGestureRecognizer:longPressGesture]; [longPressGesture release]; – Gavin Aug 15 '11 at 14:19