开发者

get the latitude and longitude from a point in MKMapView?

I just want to implement a small function that allows system to obtain a location on MKMapView where the user has 开发者_开发知识库touched. I wrote some code as following:

#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

It can work fine if I don't add a annotation on the MKMapView, otherwise the app will throw a exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[TESTViewController openCallout:]: unrecognized selector sent to instance 0x6b60180'

Any ideas?

Thanks a lot!


Your code looks fine to me. What does the call stack look like at the exception point? Do you have any calls to the apparently nonexistent selector -openCallout:?


Not entirely related, but you should implement touch-handling in touchesBegan:withEvent:. Spontaneously adding an annotation in hitTest:withEvent: is dubious at best, since it'll probably add a view to the map view...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜