简体   繁体   中英

Get Latitude and Longitude from Annotation view

How do I get the latitude and longitude of an annotationView placed on my mapView? I need to get it when the rightAccessory gets tapped.

The - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control method does return the MKAnnotationView which has a coordinate property to be used. But I want the lat and long. How do I convert the coordinate to lat and long?

Thanks.

coordinate is a CLLocationCoordinate2D , which is a struct containing latitude and longitude .

coordinate is actually a property of MKAnnotation . MKAnnotationView has an MKAnnotation property named annotation .

So, in the method you mentioned you should be able to get to the latitude using [[view annotation] coordinate].latitude and longitude using [[view annotation] coordinate]. longitude [[view annotation] coordinate]. longitude .

Add MKMapViewDelegate.

 func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView){      

let currentAnnotationLattitude = view.annotation?.coordinate.latitude

let currentAnnotationLongitude = view.view.annotation?.coordinate.longitude
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM