How can i get any information like lat,long when i touch on MKMapView in iPhone/iPad?
I have a mapView using xib file now when i touch in the mapview i want the latitude and longitude开发者_运维知识库 of that particular area so there any whey or any sample code which help me in this task.Thanks in Advance.
With iOS 3.2 or greater, it's probably better and simpler to use a UIGestureRecognizer
with the map view instead of trying to subclass it and intercepting touches manually.
First, add the gesture recognizer to the map view:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapGestureHandler:)];
tgr.delegate = self; //also add <UIGestureRecognizerDelegate> to @interface
[mapView addGestureRecognizer:tgr];
[tgr release];
Next, implement shouldRecognizeSimultaneouslyWithGestureRecognizer
and return YES
so your tap gesture recognizer can work at the same time as the map's (otherwise taps on pins won't get handled automatically by the map):
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Finally, implement the gesture handler:
- (void)tapGestureHandler:(UITapGestureRecognizer *)tgr
{
CGPoint touchPoint = [tgr locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate
= [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
NSLog(@"tapGestureHandler: touchMapCoordinate = %f,%f",
touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}
Its kind of a tricky thing to be done.
First you need to subclass mkmapview
in
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method you can find the location of touch and then using this method
- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view
you can find lat and long..
In Swift 3:
func tapGestureHandler(_sender: UITapGestureRecognizer) {
let touchPoint = _sender.location(in: mapView)
let touchMapCoordinate = mapView.convert(touchPoint, toCoordinateFrom:mapView)
print("latitude: \(touchMapCoordinate.latitude), longitude: \(touchMapCoordinate,longitude)")
}
精彩评论