【问题标题】:get the latitude and longitude from a point in MKMapView?从 MKMapView 中的某个点获取纬度和经度?
【发布时间】:2010-08-16 18:32:19
【问题描述】:

我只想实现一个小功能,允许系统获取用户触摸的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上加注解也可以正常工作,否则app会抛出异常:

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

有什么想法吗?

非常感谢!

【问题讨论】:

    标签: iphone location mkmapview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2019-11-05
        • 1970-01-01
        • 2021-04-16
        • 2020-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-31
        • 2019-03-25
        相关资源
        最近更新 更多