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

I would like to offer the user the option to (manually) select lat. and for a long time. coordinates by touching MKMapView. How can i achieve this?

I saw the delegate MKMapViewsuggest a method convertPoint:toCoordinateFromView:. I think this may be a good start, but I don’t know how to create a touch point.

I would appreciate any help. Thank.

+5
source share
2 answers

A UITouchobject (see here ) has an API:

- (CGPoint)locationInView:(UIView *)view

Then use the API MKMapViewthat you have identified.

+6
source

vwMapis the name of the object MKMapview:

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];
}
+10

All Articles