Jonathan Gurebo
Jonathan Gurebo

Reputation: 285

Get GPS Coordinates from Map View using Gesture Recognizer (LongPress)

I want to get the GPS Coordinates from a map view when the user long presses in the view.

Currently i'm using:

- (void)viewDidLoad
{
    [super viewDidLoad];
    mapView.showsUserLocation =YES;
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)];
    longPressGesture.minimumPressDuration = 1.5;
    [mapView addGestureRecognizer:longPressGesture];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

        CLLocationCoordinate2D coordinate;
        coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];// how to convert this to a String or something else?
        NSLog(@"Longpress");
    }
}

But how do i get the Coordinates? from the coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];?

Upvotes: 1

Views: 1294

Answers (1)

AlexWien
AlexWien

Reputation: 28747

To answer your question in code:

"how to convert this to a String or something else?"

NSLog(@"LongPress coordinate: latitude = %f, longitude  = %f", coordinate.latitude, coordinate.longitude);

A geographical coordinate is represented by latitdue and longitude.
Using ios you use CLLocationCoordinate2D which contains the coordinate components latitude and longitude.

latitdue has range [-90.0, 90.0]
longitude has range [-180.0, 180.0]

You can use CLLocationCoordinate2D or your own struct to store it.

Upvotes: 1

Related Questions