gpt4 book ai didi

swift - didEnterRegion 触发我所有的地理围栏

转载 作者:IT王子 更新时间:2023-10-29 05:27:27 24 4
gpt4 key购买 nike

当 GPS 进入定义的区域时,我所有的地理围栏都会触发,起初我以为是因为半径,但即使在减半后我也遇到了同样的问题。

import UIKit
import CoreLocation


class itemDesc {
var title: String
var coordinate: CLLocationCoordinate2D
var regionRadius: CLLocationDistance
var location: String
var type: String

init(title: String, coordinate: CLLocationCoordinate2D, regionRadius: CLLocationDistance, location: String, type: String) {
self.title = title
self.coordinate = coordinate
self.regionRadius = regionRadius
self.location = location
self.type = type
}

}

class ViewController: UIViewController, CLLocationManagerDelegate {

let locationManager = CLLocationManager()

override func viewDidLoad() {

super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyBest

setupData()
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

}

func locationManager(manager: CLLocationManager, monitoringDidFailForRegion region: CLRegion?, withError error: NSError) {
print("Monitoring failed for region with identifier: \(region!.identifier)")
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Location Manager failed with the following error: \(error)")
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}

func handleRegionEvent(region: CLRegion!) {
print("Geofence triggered \(region.identifier)")
}

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLCircularRegion {
handleRegionEvent(region)
}
}

func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
if region is CLCircularRegion {

}
}

func setupData(){
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) {

let itemRegion = [
itemDesc( title: "DOOR", coordinate: CLLocationCoordinate2DMake(00.497699, 00.575095), regionRadius: 0.5, location: "DOOR", type: "exterior"),
itemDesc( title: "BARN FRONT", coordinate: CLLocationCoordinate2DMake(00.49751, 00.575149), regionRadius: 0.5, location:"BARN FRONT", type: "exterior"),
itemDesc( title: "GRASS", coordinate: CLLocationCoordinate2DMake(00.497337, 00.575069), regionRadius: 0.5, location: "GRASS ", type: "nature")]

for item in itemRegion {

let coordinate = item.coordinate
let regionRadius = item.regionRadius
let title = item.title
let region = CLCircularRegion(center: coordinate, radius: regionRadius, identifier: title)

locationManager.startMonitoringForRegion(region)

}
} else{
print("system can't track regions")
}

}

}

使用 (0.497337, 0.575069) 我只希望触发 GRASS 栅栏,但这并没有发生。

输出:

区域半径 = 1.0

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

区域半径 = 0.5

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

虽然即使在 1 米处这也不应该成为问题:

The fourth decimal place is worth up to 11 m

The fifth decimal place is worth up to 1.1 m

The sixth decimal place is worth up to 0.11 m

最佳答案

GPS 芯片和 kCLLocationAccuracyBestForNavigation 的最佳精度通常只有 10 米。

Apple 表示(在位置和 map PG 中)区域的最小距离应假定为 200 米

正如这个答案所指出的那样 - 它会有所帮助但不会取悦你

https://stackoverflow.com/a/23931552/2027018

关于swift - didEnterRegion 触发我所有的地理围栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38887019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com