swift 如何清除所有地理围栏并根据用户的位置设置新的地理围栏?

nnsrf1az  于 2023-03-22  发布在  Swift
关注(0)|答案(1)|浏览(194)

基本上,我想发生的是当用户启动应用程序时,执行“getNearbyLocations”。这段代码已经到位。然后我们清除所有现有的围栏,并为最近的商店设置新的围栏。这样,如果用户接近商店,地理围栏就会踢应用程序,然后我们可以使用lat/long然后签入。在我目前拥有的代码中,lat/long是硬编码的:

//Location info for check -in
    var latitude: CLLocationDegrees = 32.991652
    var longitude: CLLocationDegrees = -96.8511617
    var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var radius: CLLocationDistance = CLLocationDistance(10.0)
    var identifier: String = "storeID"
    var geoRegion: CLCircularRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)

    geoRegion.notifyOnEntry = true
    geoRegion.notifyOnExit = false

    // Core Location manager
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
    manager.startMonitoringForRegion(geoRegion)

如何设置它,使地理围栏自动清除,并根据用户的位置设置新的?

jhiyze9q

jhiyze9q1#

这个应该可以

locationManager.monitoredRegions.forEach { region in
  if region is CLCircularRegion {
    locationManager.stopMonitoring(for: region) // This line does the job
  }
}

相关问题