首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用CLCircularRegion SWIFT设置区域?地理围栏

如何使用CLCircularRegion SWIFT设置区域?地理围栏
EN

Stack Overflow用户
提问于 2018-08-27 16:23:43
回答 1查看 739关注 0票数 -3

我正在使用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)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-28 06:07:49

实际上,您并不是从任何地方开始监控。您请求授权,但实际上并不处理成功(或失败!)这一请求。

您可以(部分地)通过监视授权成功来完成此操作:

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可能很快就会导致疯狂:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52035293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档