【问题标题】:Location Manager is not updating location位置管理器不更新位置
【发布时间】:2013-07-30 20:46:00
【问题描述】:

我有以下代码:

- (void) viewWillAppear {
    [self startSignificantChangeUpdates];
}



- (void)startSignificantChangeUpdates
{


    if ([CLLocationManager locationServicesEnabled])
    {
        NSLog(@"start significant changes");

        if (nil == locationManager)
            locationManager = [[CLLocationManager alloc] init];

        locationManager.delegate = self;
        [locationManager startMonitoringSignificantLocationChanges];
    }
}


problem is that the location manage is not  calling its delegate function 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

我能做什么?

【问题讨论】:

  • 它可能无法解决您的问题,但我建议使用 didUpdateLocations: 而不是 didUpdateToLocation:fromLocation(请参阅 stackoverflow.com/questions/12602463/…),因为 Apple 已弃用它。

标签: ios cllocationmanager cllocation


【解决方案1】:

你如何测试这个?如果你在模拟器中运行它,你可以去 Debug->Location->Freeway Drive。您可以在 locationManager:didUpdateToLocation 方法中放置一个断点,甚至可以放置一条日志语句来记录当前位置坐标。你应该会看到它起作用了。

【讨论】:

  • 它无法立即获取您当前的位置?为什么要等到你改变你的位置?我可以改变吗?
  • 它确实获得了您当前的位置。不过可能需要几秒钟才能得到它。在当前位置之后,需要对位置进行重大更改才能发生报告(例如手机信号塔更改)。因此,在高速公路上行驶时,模拟器可以帮助您检测重大更新。
  • 问题是由于某种原因,即使几秒钟后它仍然无法获取用户的当前位置......
【解决方案2】:

在您的 .h 文件中:

#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

在您的 .m 文件中:

@implementation ViewController {
    // This is a private variable, used within this file only
    CLLocationManager *locationManager;
}

-(void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
}

-(void)viewWillAppear {
    // If the delegate is still not being set, try putting this code into viewDidAppear
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError: %@", error);
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"didUpdateToLocation: %@", [locations lastObject]);
    CLLocation *currentLocation = [locations lastObject];
    [locationManager stopUpdatingLocation];
}

有关获取用户位置的更多信息,请查看本指南,我曾经在我的应用中实现位置:http://www.appcoda.com/how-to-get-current-location-iphone-user/

【讨论】:

  • 对我来说完美的答案。
【解决方案3】:

请替换此行

[locationManager startMonitoringSignificantLocationChanges]; 

使用此代码

[locationController.locationManager startUpdatingLocation];

然后检查

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多