【问题标题】:How set a region using CLCircularRegion SWIFT? Geofencing如何使用 CLCircularRegion SWIFT 设置区域?地理围栏
【发布时间】:2019-02-01 18:06:42
【问题描述】:

我正在与 SWIFT 上的CoreLocation 合作,我想创建一个地理区域,我可以在其中查看用户是否在正确的位置。用户位于此坐标的示例:Lat: 44.8856828 and Lon: -93.2131653。我想在他周围设置一个200 米的区域,以确保数据正确,并且用户可以使用200-meter 区域检查该地点的任何部分。这就是我在代码中必须要做的!

override func viewDidLoad() {
    latLabel.text = latStore
    lonLabel.text = lonStore
    readData()
    regionLocation()
}

//Accessing the are around the user

func regionLocation() {
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
}

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    print("You enter the place")
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    print("Your exit the place")
}

func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
    print("Start monitoring")
    locationManager.requestAlwaysAuthorization()

    let currentRegion = CLCircularRegion(center: (locationManager.location?.coordinate)!, radius: 200, identifier: "Place")
    locationManager.startMonitoring(for: currentRegion)
}

【问题讨论】:

  • 您的做法是正确的,您面临哪些问题?也请将您的代码发布为代码/文本而不是图像。
  • @lorenzoliveto 在我的代码中,当我更改位置时,控制台中没有显示用户已离开 200 周长

标签: ios swift core-location latitude-longitude


【解决方案1】:

您实际上并没有在任何地方开始监控。您请求授权,但实际上并未处理该请求的成功(或失败!)。

您可以(部分)通过监控授权成功来做到这一点:

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if(status == .authorizedAlways) {
        let currentRegion = CLCircularRegion(center: (locationManager.location?.coordinate)!, radius: 200, identifier: "Place")
        locationManager.startMonitoring(for: currentRegion)
    }
}

但这不适用于您已经获得授权的情况。在请求授权之前,您确实应该检查当前的授权状态。

您也真的不想从didStartMonitoring 内部请求授权,而从didStartMonitoring 内部调用startMonitoring 可能很快会导致精神错乱:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多