Ios Map: Getting latitude and longitude

2019-08-12 23:17发布

问题:

Am trying to read the longitude and latitude when the user touch (tap) on the ios Map place i want to get that particular (user tapping place) place latitude and longitude points. (for (e.x) if the user touch the ios head office means i want to show ios head office place longitude and latitude points). How can i do this. Any one can help me to solve this issue.

回答1:

Override a touch handler (touchesBegan or UITapGestureRecognizer) and then use - (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view in MKMapView, class reference here. Then add an appropriate annotation to the map using a class conforming to the MKAnnotation protocol, e.g. MKPinAnnotationView.

So an example touch handler method would look something like:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    CGPoint pt = [[touches  anyObject]locationInView:self.mapView];  
    CLLocationCoordinate2D latLong = [self.mapView convertPoint:pt toCoordinateFromView:self.mapView]; 
    //add code here for annotation 
}

Then add the annotation - this tutorial http://www.raywenderlich.com/21365/introduction-to-mapkit-in-ios-6-tutorial should help you with that- there's an iOS5 version on the same site as well.