CLLocationManager不更新位置

时间:2012-08-01 05:58:16

标签: objective-c mapkit core-location

问题是位置管理员没有更新位置。 调用(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中获得的值(位置管理器中的@“确实更新%f”,newLoc.coordinate.latitude)总是相同的 - 尽管我从当前位置开始移动了超过2公里。

如果有locationupdate,请帮助我了解如何获取确切的新位置。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您正在使用

停止位置管理器
[self.locationManager stopUpdatingLocation];

in

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

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