How can i get any information like lat,long when i touch on MKMapView in iPhone/iPad?
Asked Answered
W

3

1

I have a mapView using xib file now when i touch in the mapview i want the latitude and longitude of that particular area so there any whey or any sample code which help me in this task.Thanks in Advance.

Whomever answered 2/9, 2011 at 12:18 Comment(0)
O
6

With iOS 3.2 or greater, it's probably better and simpler to use a UIGestureRecognizer with the map view instead of trying to subclass it and intercepting touches manually.

First, add the gesture recognizer to the map view:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
        initWithTarget:self action:@selector(tapGestureHandler:)];
tgr.delegate = self;  //also add <UIGestureRecognizerDelegate> to @interface
[mapView addGestureRecognizer:tgr];
[tgr release];

Next, implement shouldRecognizeSimultaneouslyWithGestureRecognizer and return YES so your tap gesture recognizer can work at the same time as the map's (otherwise taps on pins won't get handled automatically by the map):

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer
        :(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Finally, implement the gesture handler:

- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
    CGPoint touchPoint = [tgr locationInView:mapView];

    CLLocationCoordinate2D touchMapCoordinate 
        = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];

    NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f", 
        touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}
Outdo answered 2/9, 2011 at 14:13 Comment(0)
E
1

Its kind of a tricky thing to be done.

First you need to subclass mkmapview

in

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

method you can find the location of touch and then using this method

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

you can find lat and long..

Entwine answered 2/9, 2011 at 12:27 Comment(0)
B
0

In Swift 3:

func tapGestureHandler(_sender: UITapGestureRecognizer) {
    let touchPoint = _sender.location(in: mapView)
    let touchMapCoordinate = mapView.convert(touchPoint, toCoordinateFrom:mapView)

    print("latitude: \(touchMapCoordinate.latitude), longitude: \(touchMapCoordinate,longitude)")
}
Belita answered 8/10, 2017 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.