ios - locationManager:didEnterRegion 在提供的区域中输入时未调用
问题描述
我正在尝试在 iOS 中使用 GoogleMaps 实现地理围栏。我已经使用以下代码初始化了具有所需属性的 CLLocationManager 对象,还提供了开始监视的区域。
private func setupGoogleMapServicecs() {
let camera = GMSCameraPosition.camera(withLatitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude), zoom: 6.0)
let mapRect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
mapView = GMSMapView.map(withFrame: mapRect, camera: camera)
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.delegate = self
self.view.addSubview(mapView)
let circleCenter = CLLocationCoordinate2D(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))
//-33.86 & 151.20
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = circleCenter
marker.title = "Title"
marker.snippet = "Snippet"
marker.icon = UIImage(named: "ico-pin-map")
marker.map = mapView
let circ = GMSCircle(position: circleCenter, radius: CLLocationDistance(circularRadius))
circ.fillColor = UIColor(red: 0.35, green: 0, blue: 0, alpha: 0.05)
circ.strokeColor = .red
circ.strokeWidth = 5
circ.map = mapView
self.registerLocationManager()
}
private func registerLocationManager() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.distanceFilter = 50
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
let centrePosition = CLLocationCoordinate2D(latitude: CLLocationDegrees(latitude), longitude: CLLocationDegrees(longitude))
region = CLCircularRegion(center: centrePosition, radius: CLLocationDistance(circularRadius), identifier: "test")
region.notifyOnEntry = true
region.notifyOnExit = true
self.locationManager.startMonitoring(for: region)
}
此外,我实现了以下委托方法来测试从提供的区域进入/退出时可以触发哪个委托方法。
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("test")
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
//Testing Code
let alert = iAlert()
alert.showSingleOptionOkAlertWith(Title: "Geofencing", message: "didRangeBeacons", presenter: self)
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("The monitored regions are: \(manager.monitoredRegions)")
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
//Testing Code
let alert = iAlert()
alert.showSingleOptionOkAlertWith(Title: "Geofencing", message: "didExitRegion", presenter: self)
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
//Testing Code
let alert = iAlert()
alert.showSingleOptionOkAlertWith(Title: "Geofencing", message: "didEnterRegion", presenter: self)
}
仅当我在指定区域站立 5 分钟时,此方法称为“didEnterRegion”,但之后不再调用它。请建议我是否错过了任何步骤或其他我可以尝试的方法?我想将它集成到GoogleMap上,我确实尝试过MKMapKit并且它正在工作。
解决方案
推荐阅读
- c++ - C++:MSC 不解析模板运算符(gcc 和 clang ok)
- sql - 带有横向连接的查询返回一个带括号的值。如何删除它?
- python - 用指数定律拟合数据
- android-studio - 我应该如何在加载我的启动画面图像时在后台加载我的 webview,因为它长时间显示白屏
- python - OpenPyXl:如何使图表的背景透明?
- zoho - 模块“zcrmsdk”没有属性“ZCRMRestClient”
- javascript - 使用 Express 和 Peerjs 获取 Cookie
- java - 在线程中处理后输出结果
- python - Connecting using Python Paramiko through jump host that offers an interactive selection of target servers
- omnet++ - parameters values of a selected module in properties view are disappearing in OMNeT++