I have a mapView outlet on my view (MKMapViewDelegate) with “Shows user location” enabled.
In the viewdidload for that controller I have
CLLocation *userLoc = mapView.userLocation.location;
CLLocationCoordinate2D userCoordinate = userLoc.coordinate;
NSLog(@"user latitude = %f",userCoordinate.latitude);
NSLog(@"user longitude = %f",userCoordinate.longitude);
mapView.delegate=self;
The simulator shows the correct user location on the map (I’m using Custom location in the IOS Simulator)
But the NSLog shows 0 and 0 for latitude and longitude.
Shouldn’t I be able to get the custom longitude and latitude in the simulator?
UPDATE WITH ANSWER:
Needed to implement
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
self.mapView.centerCoordinate = userLocation.location.coordinate;
}
Location services aren’t instantaneous. Your code is in
viewDidLoad, so it hasn’t had a chance to get a fix on your position yet. You should set thedelegateproperty for the map view to an object that implements theMKMapViewDelegateprotocol.