2012-08-01 48 views
1

问题是位置管理器未更新位置。 (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公里。

请帮助我如何获取确切的新位置,只要有位置更新。提前致谢。

+0

你为什么重新启动使用NSTimer的可怜的位置管理器? **如果你已经阅读过文档,你会发现只需调用**'startUpdatingLocation' **就可以了!** – 2012-08-01 06:02:36

+0

在添加一个定时器之前,我试图在视图中调用一次[locationManager startUpdatingLocation] ,但在locationUpdate委托方法中,我得到了与旧的一样的新位置。 – Sonal0706 2012-08-01 06:31:32

+0

这可能是因为......欧盟......你不动? – 2012-08-01 07:54:47

回答

3

您在

-(void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLoc 
     fromLocation:(CLLocation *)oldLoc 

[self.locationManager stopUpdatingLocation]; 

停止位置管理。这种委托方法在用户每次移动时都会被调用,并且可以在开始时为您提供旧的(缓存的)数据,所以当您获得第一次定位修复时,您可以立即停止它,您可能每次都会获得一个缓存位置。修复很简单,只是不要停止位置管理器,而是当你的viewController消失或在类似的有用的地方。

+0

感谢您的回复。其实我正在测试iPad的应用程序与无线..并且因为它没有一个GPS芯片,我没有得到任何位置更新...代码在iphone上工作... – Sonal0706 2012-08-08 06:00:37