问题是位置管理器没有更新位置。函数被调用,(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLoc fromLocation:(CLLocation *)oldLoc
但它将新位置显示为相同的旧位置。
以下是我的一段代码: 在我的 viewDidLoad 方法中,我正在创建 CLLocationManager 的对象
-(void) viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// created a timer to call locationUpdate method
[NSTimer scheduledTimerWithTimeInterval:20 target: self selector: @selector(locationUpdate) userInfo: nil repeats: YES];
}
-(void)locationUpdate
{
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
fromLocation:(CLLocation *)oldLoc
{
NSLog(@"in locationmanager did update %f",newLoc.coordinate.latitude);
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLoc.coordinate, 0.01, 0.02);
[self.mapView setRegion:region animated:YES];
[self.locationManager stopUpdatingLocation];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.02);
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.userLocation.coordinate, span);
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
}
我在 NSLog(@"in locationmanager did update %f",newLoc.coordinate.latitude) 中得到的值总是相同的——尽管我从当前位置开始移动了超过 2 公里。
请帮助我了解如何在有位置更新时获得确切的新位置。提前致谢。