【问题标题】:Get location updates in background - only for Country change iOS在后台获取位置更新 - 仅适用于国家/地区更改 iOS
【发布时间】:2019-01-13 03:28:56
【问题描述】:

仅当国家/地区发生变化时,核心位置获取后台位置更新的最佳方式是什么?

【问题讨论】:

标签: ios swift core-location background-mode


【解决方案1】:

您可以使用CLGeocoderreverseGeocodeLocation 来获取您所在位置的当前国家/地区。

func locationManager(_ manager: CLLocationManager, didUpdateLocations objects: [CLLocation]) {
    let location = objects.last!
    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(location!) { (places, error) in
            if(error == nil){
                if(places != nil){
                    let place: CLPlacemark = places![0]
                    let country = place.country
                    // do something if its changed

                }
            } else {
                //handle error
            }

但问题是您需要监控位置才能发生这种情况。您可以使用startMonitoringSignificantLocationChanges 作为一种选择,也可以将所需的精度设置为kCLLocationAccuracyThreeKilometers 这样的大值,这两者都将减少位置更新使用的电量。

【讨论】:

    【解决方案2】:

    您要查找的内容称为 geofencing,有很多关于它的文章。您需要以任何方式实现诸如

    之类的功能

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) (CLLocationManagerDelegate)

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) (CLLocationManagerDelegate)

    open func requestAlwaysAuthorization() (CLLocationManager)

    func startMonitoring(for region: CLRegion) (CLLocationManager)

    【讨论】:

      猜你喜欢
      • 2012-01-21
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      相关资源
      最近更新 更多