Swift 我为我所有的地理围栏开火了吗

Swift 我为我所有的地理围栏开火了吗,swift,core-location,cllocationmanager,cllocation,Swift,Core Location,Cllocationmanager,Cllocation,当GPS进入一个确定的区域时,我所有的地理围栏都会被触发,起初我以为这是因为半径的缘故,但即使将其减半,我也会遇到同样的问题 导入UIKit 导入核心定位 类itemDesc{ 变量标题:字符串 变量坐标:CLLocationCoordinate2D 变量区域半径:CLLocationDistance 变量位置:字符串 变量类型:字符串 初始(标题:字符串,坐标:CLLocationCoordinate2D,区域半径:CLLocationDistance,位置:字符串,类型:字符串){ self

当GPS进入一个确定的区域时,我所有的地理围栏都会被触发,起初我以为这是因为半径的缘故,但即使将其减半,我也会遇到同样的问题

导入UIKit
导入核心定位
类itemDesc{
变量标题:字符串
变量坐标:CLLocationCoordinate2D
变量区域半径:CLLocationDistance
变量位置:字符串
变量类型:字符串
初始(标题:字符串,坐标:CLLocationCoordinate2D,区域半径:CLLocationDistance,位置:字符串,类型:字符串){
self.title=标题
self.coordinate=坐标
self.regionRadius=regionRadius
self.location=位置
self.type=type
}
}
类ViewController:UIViewController、CLLocationManagerDelegate{
让locationManager=CLLocationManager()
重写func viewDidLoad(){
super.viewDidLoad()
locationManager.delegate=self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy=KCallocationAccuracyBest
setupData()
}
func locationManager(经理:CLLocationManager,didChangeAuthorizationStatus:CLAuthorizationStatus){
}
func locationManager(管理器:CLLocationManager,monitoringDidFailForRegion区域:CLRegion?,错误:NSError){
打印(“对标识符为\(region!.identifier)的区域的监视失败”)
}
func locationManager(管理器:CLLocationManager,错误:N错误){
打印(“位置管理器失败,错误如下:\(错误)”)
}
func locationManager(管理器:CLLocationManager,didUpdateLocations位置:[CLLocation]){
让locValue:CLLocationCoordinate2D=manager.location!.coordinate
打印(“位置=\(locValue.latitude)\(locValue.latitude)”)
}
func handleRegionEvent(区域:CLRegion!){
打印(“地理围栏触发\(region.identifier)”)
}
func locationManager(经理:CLLocationManager,区域:CLRegion){
如果区域是循环区域{
HandlerRegionEvent(区域)
}
}
func locationManager(经理:CLLocationManager,地区:CLRegion){
如果区域是循环区域{
}
}
func setupData(){
如果类的CLLocationManager.IsMonitoringAvailable(CLCircularRegion.self){
设itemRegion=[
itemDesc(标题:“门”,坐标:CLLocationCoordinate2DMake(00.497699,00.575095),区域半径:0.5,位置:“门”,类型:“外部”),
itemDesc(标题:“谷仓正面”,坐标:CLLocationCoordination2DMake(00.49751,00.575149),区域半径:0.5,位置:“谷仓正面”,类型:“外部”),
itemDesc(标题:“草地”,坐标:CLLocationCoordinate2DMake(00.497337,00.575069),区域半径:0.5,位置:“草地”,类型:“自然”)]
对于itemRegion中的项目{
让坐标=item.coordinate
设regionRadius=item.regionRadius
设title=item.title
让区域=CLCircularRegion(中心:坐标,半径:区域半径,标识符:标题)
locationManager.startMonitoringForRegion(地区)
}
}否则{
打印(“系统无法跟踪区域”)
}
}
}
使用(0.497337,0.575069),我只希望触发草地围栏,但这不会发生

产出:

区域半径=1.0

区域半径=0.5

尽管即使是在1米的时候,这也不应该是一个问题:

小数点后第四位的值高达11米

小数点后第五位的值最多为1.1米

小数点后第六位的值最多为0.11米


GPS芯片和KCallocationAccuracy的最佳导航精度通常只有10米

苹果表示(在位置和地图页面中)区域的最小距离应假定为200米

正如这个答案所指出的,这会有帮助,但不会让你高兴


如果它能帮助那些偶然发现这一点的人,那么我在CLRegion上就没什么进展了

相反,我使用了CLLocation类和use,计算出了我的每个区域/位置的距离

locations = 37.33233141 -122.0312186
locations = 37.33233141 -122.0312186
locations = 0.497337 0.575069
Geofence triggered BARN FRONT
Geofence triggered DOOR
Geofence triggered GRASS
locations = 37.33233141 -122.0312186
locations = 37.33233141 -122.0312186
locations = 0.497337 0.575069
Geofence triggered BARN FRONT
Geofence triggered DOOR
Geofence triggered GRASS