sayguh
sayguh

Reputation: 2550

MKMapView userLocation showing 0,0 in simulator?

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;
} 

Upvotes: 12

Views: 5014

Answers (3)

ericsoco
ericsoco

Reputation: 26253

I had to set a location in the simulator, via Debug > Location. It was set to None, which dumped me at (0°, 0°) and never updated; you can set a Custom Location or choose from other simulated location scripts that move the user location automatically over time.

Upvotes: 0

kalyan711987
kalyan711987

Reputation: 11

MKMapView *mapview=[[MKMapView alloc]initWithFrame:CGRectMake(0,0,300, 364)];
mapview.delegate=self;
mapview.showsUserLocation=YES;

Which then will call the mapkit delegate, in the mak kit delegate try to print the coordinates. NSLog(@"user latitude = %f",userLocation.location.coordinate.latitude); NSLog(@"user longitude = %f",userLocation.location.coordinate.longitude);

Upvotes: 0

Jim
Jim

Reputation: 73936

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.

Upvotes: 14

Related Questions