首页 > 解决方案 > 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)
  }

此外,我已经像这样在 info.plist 中配置了键 在此处输入图像描述

此外,我实现了以下委托方法来测试从提供的区域进入/退出时可以触发哪个委托方法。

    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并且它正在工作。

标签: iosswiftgoogle-mapscore-locationgeofencing

解决方案


推荐阅读