问题描述
|
我正在制作一个应用程序,打开它时,我开始获取用户位置并将地图居中放置在用户位置:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
那我有
- (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];
}
}
使用isopening标志,地图会在获得完全准确性之前停止居中,但如果我未设置标志,则地图将一遍又一遍地居中。有没有一种方法可以获取位置并完全准确地对中地图,然后停止对中?
解决方法
这是我的方法:
- (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