Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 972
jcdmb Avatar asked Aug 10 '10 17:08

jcdmb


2 Answers

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];
}
like image 83
pkc456 Avatar answered Oct 17 '22 19:10

pkc456


A UITouch object (see here) has the API:

- (CGPoint)locationInView:(UIView *)view

Then use the MKMapView API you identified.

like image 42
Eric Avatar answered Oct 17 '22 19:10

Eric