当我在iPhone / iPad上触摸MKMapView时,我怎样才能获得像lat这样的信息? [英] How can i get any information like lat,long when i touch on MKMapView in iPhone/iPad?

查看:78
本文介绍了当我在iPhone / iPad上触摸MKMapView时,我怎样才能获得像lat这样的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个mapView使用xib文件当我在mapview中触摸时我想要特定区域的纬度和经度,所以有任何乳清或任何示例代码可以帮助我完成这项任务。谢谢你。

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.

推荐答案

使用iOS 3.2或更高版本,使用 UIGestureRecognizer 使用地图视图,而不是尝试将其子类化并手动截取触摸。

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];

接下来,实现 shouldRecognizeSimultaneouslyWithGestureRecognizer 并返回因此您的点击手势识别器可以与地图同时工作(否则地图上的点击不会被地图自动处理):

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

这篇关于当我在iPhone / iPad上触摸MKMapView时,我怎样才能获得像lat这样的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆