从MKMapView中的某个点获取经度和纬度?

时间:2010-08-16 18:32:20

标签: iphone location mkmapview

我只想实现一个小功能,允许系统获取用户触摸的MKMapView上的位置。我写了一些代码如下:

#import "UIViewTouch.h"


@implementation UIViewTouch
@synthesize viewTouched;

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *) event{
    NSLog(@"Hit Test");
    NSLog(@"x = %f, y = %f", point.x, point.y);
    MKMapView *mapView = (MKMapView *) [self.subviews objectAtIndex:0];
    CLLocationCoordinate2D coordinate = [mapView convertPoint:point toCoordinateFromView:self];
    NSLog(@"Lat = %f, Lng = %f", coordinate.latitude,coordinate.longitude);
        //MapAnnotation is a custom class and confirms to MKAnnotation.
    MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:coordinate];
    [mapView addAnnotation:annotation];
    return [super hitTest:point withEvent:event];
}

- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return [super pointInside:point withEvent:event];
}

@end

如果我不在MKMapView添加注释,它可以正常工作,否则应用程序将抛出异​​常:

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [TESTViewController   openCallout:]:无法识别的选择器发送到实例0x6b60180'

有什么想法吗?

非常感谢!

2 个答案:

答案 0 :(得分:0)

您的代码对我来说很好。调用堆栈在异常点处看起来是什么样的?你有没有调用显然不存在的选择器-openCallout:

答案 1 :(得分:0)

不完全相关,但你应该在touchesBegan:withEvent:中实现触摸处理。在hitTest:withEvent中自发添加注释:充其量是可疑的,因为它可能会为地图视图添加一个视图......