开发者

iPhone MapKit: select location (coordinates) manually by touching MKMapView

I would like to offer the user the possibility to (manually) select lat. and long. coordinates by touching a 开发者_如何学编程MKMapView. How can I achieve that?

I've seen that the MKMapView delegate offers the method convertPoint:toCoordinateFromView: . I think, that could be a good starting, but I don't know how to create a point from a touch action.

I would appreciate any help. Thanks.


vwMap is the name of MKMapview object:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)]; 
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1; 
[vwMap addGestureRecognizer:tapRecognizer];



-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer {
    CGPoint point = [recognizer locationInView:vwMap];
    CLLocationCoordinate2D tapPoint = [vwMap convertPoint:point toCoordinateFromView:vwMap];

    MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init]; 
    point1.coordinate = tapPoint;

    [vwMap addAnnotation:point1];
}


A UITouch object (see here) has the API:

- (CGPoint)locationInView:(UIView *)view

Then use the MKMapView API you identified.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜