【问题标题】:Issue in getting current location获取当前位置的问题
【发布时间】:2014-04-01 10:05:05
【问题描述】:

我想获取设备的当前位置。代码正常工作。如果用户没有更改位置服务的应用授权状态,它会给出位置。我还可以检查用户是否拒绝了位置服务权限。

问题是当用户取消授权应用程序使用定位服务然后再次授权时。在这种情况下,在此之后,如果我尝试获取位置,它会给出 nil 尽管它调用了

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

状态为3 的委托方法,即kCLAuthorizationStatusAuthorized

获取当前位置的代码:

CLLocation * location = self.locationManager.location;

Getter 方法:

- (CLLocationManager *)locationManager
{
    if (!locationManager)
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
    }

    return locationManager;
}

CLLocationManager 委托方法:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    DLog(@"Location authorization changed : %d", status);

    // If user has denied permission for location service
    if (status == kCLAuthorizationStatusDenied)
    {
        DLog(@"Location service denied.");

        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusNotDetermined)
    {
        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusAuthorized)
    {

    }
}

对此有什么想法吗?

【问题讨论】:

  • 在上面你将 locationManager.delegate 设置为 nil 如果授权被撤销......你有没有再次将它设置为适当的类?
  • 刚刚注意到您还将 locationManager 设置为 nil...
  • 根据您在代码中的 cmets 在某处重新创建 locationManager 对象 - 您确定在设置 self.locationServiceDisabled = false 时会触发此操作吗?
  • 我没有看到你开始更新你的位置 (cllocationmanager startupdatinglocation)
  • 在苹果文档中,它是关于 locationManager 的位置属性的状态:“如果没有检索到任何位置数据,则此属性的值为 nil。”因此,您需要以某种方式更新您的 iPhone 位置!为了实现这一点,您应该调用 locationmanager startupdatinglocation... 方法!

标签: ios objective-c ios7 cllocationmanager cllocation


【解决方案1】:

self.locationManager.locationnil,因为您从未开始更新位置。

在苹果文档中,它说明了 locationManager 的 location 属性:

如果没有位置数据,则此属性的值为 nil 已取回。

因此,您需要以某种方式更新您的 iPhone 位置!

Apple Docs CLLocationManager

通常这意味着你想打电话

[self.locationManager startUpdatingLocation]

但你也可以使用

[self.locationManager startMonitoringSignificantLocationChanges]

【讨论】:

  • 我希望您在此处添加您的评论全文。这将使这个答案变得更好。谢谢。
【解决方案2】:

如果您将委托设置为 nil,您将不会收到有关授权状态更改的更新,不是吗?

self.locationManager.delegate = nil;

我认为您应该保留委托以接收授权状态更新,然后调用 startUpdatingLocation 方法以获取当前位置。

- (void)startUpdatingLocation

【讨论】:

  • 如果您愿意仔细查看我的代码,那么很明显,在 getter 方法中我再次设置了委托。
猜你喜欢
  • 2016-10-21
  • 2011-09-23
  • 1970-01-01
  • 2023-03-25
  • 2017-11-08
  • 2020-11-02
  • 2012-06-15
  • 1970-01-01
  • 2019-12-24
相关资源
最近更新 更多