Ios 与CLLocationManager的区域存在问题
我试图检测用户是否靠近或进入我想要的位置。我正在使用CLLocationManager监视此操作,但由于某些原因,它不起作用。我在当前位置,但没有打电话。这是我的密码:Ios 与CLLocationManager的区域存在问题,ios,swift,core-location,cllocationmanager,Ios,Swift,Core Location,Cllocationmanager,我试图检测用户是否靠近或进入我想要的位置。我正在使用CLLocationManager监视此操作,但由于某些原因,它不起作用。我在当前位置,但没有打电话。这是我的密码: class ViewController: UIViewController, CLLocationManagerDelegate { var locationManager = CLLocationManager() override func viewDidLoad() { super.v
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
// Register the region.
let maxDistance = locationManager.maximumRegionMonitoringDistance
let region = CLCircularRegion(center: center,
radius: maxDistance, identifier: "identifier")
region.notifyOnEntry = true
region.notifyOnExit = false
locationManager.startMonitoring(for: region)
}
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if region is CLCircularRegion {
print("entered....")
}
}
我还在plist文件中添加了隐私描述:
如何解决此问题
编辑:
RequestStateforRegion:和DidDetermingEstate:for:立即工作,但如何检查用户是否在附近或所在位置?例如,如果用户靠近或在该区域内,则执行某项操作,如果用户不在,则执行其他操作
编辑2
你想要的是一次性支票
CoreLocation随后将确定状态,然后调用委托上的函数,为您提供一个枚举,您可以在其中测试==.inside
例如:
import CoreLocation
class LocationController: CLLocationManagerDelegate {
typealias RegionMonitoringClosure = (CLRegion, Bool) -> Void
typealias RegionStateClosure = (CLRegion, CLRegionState) -> Void
private let manager = CLLocationManager()
private let stateClosure: RegionStateClosure
private let monitoringClosure: RegionMonitoringClosure
init(stateClosure: RegionStateClosure, monitoringClosure: RegionMonitoringClosure) {
manager.delegate = self
self.stateClosure = stateClosure
self.monitoringClosure = monitoringClosure
}
func addMonitoredRegion(_ region: CLRegion) {
manager.startMonitoring(for: region)
}
func remove(monitoredRegion region: CLRegion) {
manager.stopMonitoring(for: region)
}
func requestState(for region: CLRegion) {
manager.requestState(for: region)
}
// MARK: CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entered \(region)")
self.monitoringClosure(region, true)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Exited \(region)")
self.monitoringClosure(region, false)
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("Started monitoring region \(region)")
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
print("Determined state \(state) for region \(region)")
self.stateClosure(region, state)
}
}
用法:
let regionToTest: CLRegion = ... // your desired region.
let controller = LocationController(stateClosure: { region, state in
switch state {
case .inside:
print("inside region \(region)")
case .outside:
print("outside region \(region)")
}
}, monitoringClosure: { region, entered in
if entered == true {
print("Entered \(region)")
//...
}
})
controller.requestState(for: regionToTest)
你想要的是一次性支票
CoreLocation随后将确定状态,然后调用委托上的函数,为您提供一个枚举,您可以在其中测试==.inside
例如:
import CoreLocation
class LocationController: CLLocationManagerDelegate {
typealias RegionMonitoringClosure = (CLRegion, Bool) -> Void
typealias RegionStateClosure = (CLRegion, CLRegionState) -> Void
private let manager = CLLocationManager()
private let stateClosure: RegionStateClosure
private let monitoringClosure: RegionMonitoringClosure
init(stateClosure: RegionStateClosure, monitoringClosure: RegionMonitoringClosure) {
manager.delegate = self
self.stateClosure = stateClosure
self.monitoringClosure = monitoringClosure
}
func addMonitoredRegion(_ region: CLRegion) {
manager.startMonitoring(for: region)
}
func remove(monitoredRegion region: CLRegion) {
manager.stopMonitoring(for: region)
}
func requestState(for region: CLRegion) {
manager.requestState(for: region)
}
// MARK: CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Entered \(region)")
self.monitoringClosure(region, true)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Exited \(region)")
self.monitoringClosure(region, false)
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("Started monitoring region \(region)")
}
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
print("Determined state \(state) for region \(region)")
self.stateClosure(region, state)
}
}
用法:
let regionToTest: CLRegion = ... // your desired region.
let controller = LocationController(stateClosure: { region, state in
switch state {
case .inside:
print("inside region \(region)")
case .outside:
print("outside region \(region)")
}
}, monitoringClosure: { region, entered in
if entered == true {
print("Entered \(region)")
//...
}
})
controller.requestState(for: regionToTest)
如果您在模拟器上运行它,您可以看到与在设备上运行不同的结果 例如,如果您已经在定义的区域内,并使用模拟器进行测试。你可能不会总是得到想要的结果。由于某些原因,如果您在打开模拟器时要检测的区域内,那么模拟器不喜欢它 此外,在我自己使用信标进行了许多测试之后,结果表明,在模拟器上与该区域的距离并不准确。使用模拟器时经常出错。它还受到计算机和信标之间任何物理干扰的高度影响。该区域和金属中的许多其他设备会严重影响模拟器报告正确的数据。 我建议在物理设备上测试所有代码。 当您使用应用程序加载设备时。将设备从计算机上拔下,并在信标区域外打开应用程序
为了进一步更好地进行测试,您可以在应用程序上创建自己的本地调试器,以便在使用设备时,无需连接到计算机即可查看所需信息 如果您在模拟器上运行,您可以看到与在设备上运行不同的结果 例如,如果您已经在定义的区域内,并使用模拟器进行测试。你可能不会总是得到想要的结果。由于某些原因,如果您在打开模拟器时要检测的区域内,那么模拟器不喜欢它 此外,在我自己使用信标进行了许多测试之后,结果表明,在模拟器上与该区域的距离并不准确。使用模拟器时经常出错。它还受到计算机和信标之间任何物理干扰的高度影响。该区域和金属中的许多其他设备会严重影响模拟器报告正确的数据。 我建议在物理设备上测试所有代码。 当您使用应用程序加载设备时。将设备从计算机上拔下,并在信标区域外打开应用程序 为了进一步更好地进行测试,您可以在应用程序上创建自己的本地调试器,以便在使用设备时,无需连接到计算机即可查看所需信息 变化
locationManager.requestWhenInUseAuthorization()
到
同时检查您的gpx文件更改
locationManager.requestWhenInUseAuthorization()
到
同时检查您的gpx文件开始监控时,您是否已经在该区域内?@Paulw11是……然后您应该调用并实施跨境通知,并将其发送到您的位置管理器的代理对象。具体地说,位置管理器调用其委托的locationManager:didEnterRegion:或locationManager:didExitRegion:方法。正如Paulw11所说,这些委托方法是专门用于跨境的。我可能不理解您的问题,但在您收到您进入该地区的通知后,您不能简单地将当前位置到指定位置的距离计算为let distance=locationManager.location.distancefrom center吗?开始监控时您是否已经在区域内?@Paulw11 yes…..然后您应该呼叫并执行边界穿越通知,并将其发送给您的位置经理的代表对象具体地说,位置管理器调用其委托的locationManager:didEnterRegion:或locationManager:didExitRegion:方法。正如Paulw11所说,这些委托方法是专门用于跨境的。我可能不理解您的问题,但在收到您进入该地区的通知后,您不能简单地将当前位置到指定位置的距离计算为let distance=locationManager.location.distance from center吗?谢谢,很抱歉反应太晚。我尝试使用你的类,但当我在设备上运行应用程序时,它不会
根本不工作。我的意思是我没有得到任何日志。请检查我编辑的代码好吗?@Mc.Lover-Hmm。我再次更新了代码。你现在可以试试吗?对不起,我没有收到任何表明我在该地区的日志。@Mc.Lover奇怪!谢谢,很抱歉反应太晚。我尝试使用你的类,但当我在设备上运行应用程序时,它根本不工作。我的意思是我没有得到任何日志。请检查我编辑的代码好吗?@Mc.Lover-Hmm。我再次更新了代码。你现在可以试试吗?对不起,我没有收到任何表明我在该地区的日志。@Mc.Lover奇怪!