如何解决iPhone-中心地图,直到完全准确为止
| 我正在制作一个应用程序,打开它时,我开始获取用户位置并将地图居中放置在用户位置: 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。