我正在尝试使用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 上正常运行。