问题是位置管理器未更新位置。 (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLoc fromLocation:(CLLocation *)oldLoc
函数被调用,但它将新位置显示为相同的旧位置。CLLocationManager未更新位置
以下是我的一段代码: 在我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我得到(@“中的LocationManager没有更新%F”的值的对象,newLoc .coordinate.latitude)始终是相同的 - 虽然我从当前位置开始移动超过2公里。
请帮助我如何获取确切的新位置,只要有位置更新。提前致谢。
你为什么重新启动使用NSTimer的可怜的位置管理器? **如果你已经阅读过文档,你会发现只需调用**'startUpdatingLocation' **就可以了!** – 2012-08-01 06:02:36
在添加一个定时器之前,我试图在视图中调用一次[locationManager startUpdatingLocation] ,但在locationUpdate委托方法中,我得到了与旧的一样的新位置。 – Sonal0706 2012-08-01 06:31:32
这可能是因为......欧盟......你不动? – 2012-08-01 07:54:47