Iphone – Obtain user location from a MKMapView

core-locationiphonemkmapview

Is it possible to use the MKMapView's own location manager to return the users current location to pass into a webservice?

I have mapView.showsUserLocation=YES; and this does return a valid blue dot at my location, but in the simulator, its Cupertino – which is fine, but when i look at

mapView.userLocation.coordinate.latitude, its equal to 180, whereas a CLLocationManager returns the correct one, 37.3317.

I want to avoid having multiple location managers for my three tabs, so using the mapViews own would be helpful.

Thanks.

Best Answer

You can get the user location from the MKMapView. You are just missing a property in your retrieval of it. It should be:

mapView.userLocation.location.coordinate.latitude;

userLocation only stores a CLLocation location attribute and a BOOL updating attribute. You must go to the location attribute to get coordinates.

-Drew

EDIT: The MKMapView's userLocation does not update until the map has finished loading, and checking too early will return zeros. To avoid this, I suggest using the MKMapViewDelegate method -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation.

Related Topic