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

自作多情 提交于 2019-12-09 23:52:21

问题


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.


回答1:


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



回答2:


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..




回答3:


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


来源:https://stackoverflow.com/questions/7283279/how-can-i-get-any-information-like-lat-long-when-i-touch-on-mkmapview-in-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!