当我在iPhone/iPad上触摸MKMapView时,我怎样才能获得像lat这样的信息?

Ank*_*yas 1 mkmapview ios4 ios

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

小智 6

使用iOS 3.2或更高版本时,使用UIGestureRecognizer地图视图可能会更好更简单,而不是尝试将其子类化并手动拦截触摸.

首先,将手势识别器添加到地图视图中:

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

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
    shouldRecognizeSimultaneouslyWithGestureRecognizer
        :(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

最后,实现手势处理程序:

- (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);
}
Run Code Online (Sandbox Code Playgroud)