Get the coordinates of a point from mkmapview on iphone
A bit of dead thread digging but as this is the top result in Google it may be worth it:
You can use the tap and hold gesture recognizer in order to get the coordinates and drop a pin on the map. Everything is explained at freshmob
You can try this code
- (void)viewDidLoad
{
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[self.myMapView addGestureRecognizer:tapRecognizer];
}
-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.myMapView];
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
MKPointAnnotation *point1 = [[MKPointAnnotation alloc] init];
point1.coordinate = tapPoint;
[self.myMapView addAnnotation:point1];
}
All the best.
so i found a way to do it, finally. If i create a view and add a map object to it with the same frame. then listen for hit test on that view, i can call convertPoint:toCoordinateFromView: on the touch point sent, and give it the map like so:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
CLLocationCoordinate2D coord= [map convertPoint:point toCoordinateFromView:map];
NSLog(@"lat %f",coord.latitude);
NSLog(@"long %f",coord.longitude);
... add annotation ...
return [super hitTest:point withEvent:event];
}
this is pretty rough as is and as you scroll the map it still constantly calls hit test so you will need to handle that, but its a start at getting the gps coordinates from touching a map.