Abdullah Md. Zubair
Abdullah Md. Zubair

Reputation: 3324

is it possible to get lat long of annotations?

I am using iPhone native maps to my application. I have a set of lat, long , title & subtitle in a array and plotting them to map with annotations. When I click to the annotations title & subtitle are shown. Is there any way to get lat, long of a annotation when I click to the annotation ?

Upvotes: 2

Views: 2711

Answers (3)

Alvin George
Alvin George

Reputation: 14296

Swift 2:0

Add MKMapViewDelegate.

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

let currentAnnotationLattitude = view.annotation?.coordinate.latitude

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

Note: It works for multiple pin Annotations in a single map View. This delegate will provide annotation view's current lattitude and longitude.

Upvotes: 0

itZme
itZme

Reputation: 1439

You can implement the following method

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

MapAnnotation *annotation = (MapAnnotation *)view.annotation; //MapAnnotation your class     which conform to MKAnnotation

float lattude = annotation.coordinate.latitude;
float longtude = annotation.coordinate.longitude;



}

in the class which conform to MKMapViewDelegate

Upvotes: 4

Philipp
Philipp

Reputation: 1963

Yes. Implement the Method - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view of the MKMapViewDelegate - Protocol (reference). In there, you cann access the lat & long by accessing the annotation - Property of the MkAnnotationView, which conforms to the MkAnnotation - Protocol (reference) and therefore has a property called coordinate.

Update: The following code sample prints the lat/lon of an annotation after selecting it on the map:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
   NSLog(@"Latitude: %f", view.annotation.coordinate.latitude);
   NSLog(@"Longitude: %f", view.annotation.coordinate.longitude);
}

Upvotes: 6

Related Questions