iPhone - Center Map until it gets full accuracy
I'm making an app and when opening it I start getting userlocation and centering the map to the user location:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
Then I have
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
location = newLocation.coordinate;
if (isOpening) {
//Center location and set zoom on user when opening the app
MKCoordinateRegion region;
region.center = location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
isOpening = NO;
[mapView setRegion:region animated:TRUE];
}
}
Using the isOpening flag, the map stops centering before it gets full accuracy bu开发者_如何学Pythont if I don't set the flag the map keeps centering over and over. Is there a way to get the location and center the map with full accuracy and then stop centering it?
Here is how I do it:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
NSTimeInterval locationAge = -[userLocation.location.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (userLocation.location.horizontalAccuracy < 0) return;
CLLocation *location = userLocation.location;
static BOOL found = NO;
if (location && !found) {
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate,1000,1000);
[ridesMap setRegion:region animated:YES];
found = YES;
}//end if
}//end
精彩评论