我正在使用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)
}
发布于 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
可能很快就会导致疯狂:)
https://stackoverflow.com/questions/52035293
复制相似问题