Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKMapView userLocation showing 0,0 in simulator?

Tags:

ios

mkmapview

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;
} 
like image 602
sayguh Avatar asked Mar 08 '12 14:03

sayguh


1 Answers

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 the delegate property for the map view to an object that implements the MKMapViewDelegate protocol.

like image 199
Jim Avatar answered Oct 14 '22 12:10

Jim