donparalias donparalias - 4 months ago 23
iOS Question

How to get latitude , longitude from mapKit in iOS?

I have integrated to my project the mapkit framework and everything works fine. I just show in a map the users current position like this :

mapView.delegate = self;
mapView.showsUserLocation = YES;
MKUserLocation *userLocation = mapView.userLocation;
//numbers show map zoom. 500,500 = map stretches 500 meters to the North and the South of current location
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate,1000,1000);
[mapView setRegion:region animated:NO];


The problem is that i need to store to 2 variables the latitude and longitude of the current position but where are these values stored??

I tried to print the below but it gives me 0.000.

NSLog(@"Latitude: %f" , mapView.userLocation.coordinate.latitude);
NSLog(@"Longitude: %f" , mapView.userLocation.coordinate.latitude);

Answer

Implement this delegate method:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

Basically, the MapView will call this location once it has determined the user's location. If you check userLocation on the fly, it's not guaranteed.

You can find the rest of the delegate methods in the MKMapViewDelegate Protocol Reference

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intf/MKMapViewDelegate

Comments