Objective c CLLocationManager未更新位置

Objective c CLLocationManager未更新位置,objective-c,mapkit,core-location,Objective C,Mapkit,Core Location,问题是位置管理器未更新位置。 调用(void)locationManager:(CLLocationManager*)manager diddupdatetolocation:(CLLocation*)newLoc fromLocation:(CLLocation*)oldLoc函数,但它将新位置显示为相同的旧位置 以下是我的代码: 在viewDidLoad方法中,我正在创建CLLocationManager的对象 -(void) viewDidLoad { self.location

问题是位置管理器未更新位置。 调用
(void)locationManager:(CLLocationManager*)manager diddupdatetolocation:(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];

    }
-(void)viewDidLoad
{
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy=KCallocationAccuracyNearesttenmeters;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
//创建了一个计时器来调用locationUpdate方法
[NSTimer scheduledTimerWithTimeInterval:20目标:自选择器:@selector(locationUpdate)userInfo:nil repeats:YES];
}
-(无效)位置更新
{
[self.locationManager startUpdatingLocation];
}
-(无效)locationManager:(CLLocationManager*)经理
DidUpdateLocation:(CLLocation*)newLoc
fromLocation:(CLLocation*)oldLoc
{
NSLog(@“在locationmanager中未更新%f”,新位置坐标纬度);
Mk坐标区域=
MKCoordinateRegionMakeWithDistance(新位置坐标,0.01,0.02);
[self.mapView setRegion:区域动画:是];
[self.locationManager停止更新位置];
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释
{
if([annotation isKindOfClass:[MKUserLocation类]])
{
MKCoordinateSpan=MKCoordinateSpan(0.01,0.02);
MKCoordinateRegion region=MKCoordinateRegionMake(mapView.userLocation.coordinate,span);
[_MapViewSetRegion:区域动画:是];
[_MapViewRegionAtfits:region];
}
我在NSLog中得到的值(@“在locationmanager中更新了%f”,newLoc.coordinate.latitude)始终是相同的,即使我从当前位置开始移动了超过2公里


请帮助我在有位置更新时如何获得准确的新位置。提前感谢。

您正在使用停止位置管理器

[self.locationManager stopUpdatingLocation];


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

为什么要使用NSTimer重新启动这个糟糕的位置管理器?如果如果您阅读了文档,就会发现只需调用一次
startUpdatingLocation
就足够了!在添加计时器之前,我已经尝试调用[locationManager startUpdatingLocation]曾经在视图中加载过,但在locationUpdate委托方法中,我得到的新位置与旧位置相同。这可能是因为…嗯…你不移动?不…即使移动了3公里以上,我也不会在didUpdateLocation方法中得到新位置。但是当我退出应用程序并首次启动它时,只有新的l位置正在更新。非常感谢您提出的问题。我的问题的解决方案是
[NSTimer scheduledTimerWithTimeInterval:20目标:自选择器:@selector(位置更新)用户信息:无重复:是]
我遇到的问题:-无法更新位置,因此无法更新指南针。谢谢你的回复。实际上我是在iPad上用wifi测试该应用程序的。由于它没有gps芯片,所以我没有得到任何位置更新…该代码在iphone上工作。。。
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLoc
       fromLocation:(CLLocation *)oldLoc