I have a MKMapView on my app. This is iOS6.
-(void)viewDidLoad
{
.....
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"Update locations is hit");
NSLog(@"379 the locations count is %d",locations.count);
CLLocation *obj = [locations lastObject];
NSLog(@"the lat is %f", obj.coordinate.latitude);
NSLog(@"the long is %f", obj.coordinate.longitude);
NSLog(@"the horizontal accuracy is %f",obj.horizontalAccuracy);
NSLog(@"the vertical accuracty is %f",obj.verticalAccuracy);
if (obj.coordinate.latitude != 0 && obj.coordinate.longitude != 0)
{
CLLocationCoordinate2D currrentCoordinates ;
currrentCoordinates.latitude = obj.coordinate.latitude;
currrentCoordinates.longitude = obj.coordinate.longitude;
}
....more computation
[locationManager stopUpdatingLocation];
}
When I first load the app, my location is showing little far away. Some times miles away. I also have a reset location button and if I click that map shows correct location. This is what I have in reset location button click:
- (IBAction)btnResetLocationClick:(UIButton *)sender
{
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
[locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];
}
So how do I make the app get the correct current location on load up itself. Is there a way for the app to tell the map to wait for few milliseconds and then update. Or any other idea? Please let me know. If you need more information, please ask. Thanks.
What you could do is to:
didUpdateLocationsautomatically, but rather;didUpdateLocationsonly if you’re sufficiently happy with thehorizontalAccuracy; andThus,
didUpdateLocationsmight look like:And then in
viewDidLoad, turn if off after a certain period of time has passed (you might want to check some status variable that you set if you’ve already turned off location services):Original answer:
I don’t see where you’re updating your map to be around your location. I’d expect to see something like:
or like:
I’d also suggest, rather than turning off location services immediately (since frequently the first few locations are not that accurate), leave it on for a bit and let it hone in on your location until the
horizontalAccuracyandverticalAccuracyfall within a certain predetermined limit. Look at those accuracy figures for a few calls todidUpdateLocationsand you’ll see what I mean.I originally thought you were getting a negative
horizontalAccuracyat which point I suggested implementingdidFailToLocateUserWithErrorbecause according tohorizontalAccuracy, “A negative value indicates that the location’s latitude and longitude are invalid.” Hopefully you get an error that describes what the issue is. Even if you’re not currently getting a negativehorizontalAccuracy, you might want to implement this method, just to make sure you’re handling any errors correctly.