问题
I'm working on an app in which are 66 annotations. These annotations are centers of regions and whenever a user enters a region, a notification appears, but that works for only 20 of them because there's a limited number for region monitoring. My problem is I don't know how to monitor more than 20 regions. Could anyone help?
回答1:
set currentLocation
from your didUpdateLocations
var currentLocation : CLLocation?{
didSet{
evaluateClosestRegions()
}
}
var allRegions : [CLRegion] = [] // Fill all your regions
Now calculate and find the closest regions to your current location and only track those.
func evaluateClosestRegions() {
var allDistance : [Double] = []
//Calulate distance of each region's center to currentLocation
for region in allRegions{
let circularRegion = region as! CLCircularRegion
let distance = currentLocation!.distance(from: CLLocation(latitude: circularRegion.center.latitude, longitude: circularRegion.center.longitude))
allDistance.append(distance)
}
// a Array of Tuples
let distanceOfEachRegionToCurrentLocation = zip(allRegions, allDistance)
//sort and get 20 closest
let twentyNearbyRegions = distanceOfEachRegionToCurrentLocation
.sorted{ tuple1, tuple2 in return tuple1.1 < tuple2.1 }
.prefix(20)
// Remove all regions you were tracking before
for region in locationManager.monitoredRegions{
locationManager.stopMonitoring(for: region)
}
twentyNearbyRegions.forEach{
locationManager.startMonitoring(for: $0.0)
}
}
To avoid having the didSet
called too many times, I suggest you set the distanceFilter
appropriately (not too big so you would catch the region's callbacks too late and not too small so that you won't have redundant code running). Or as this answer suggests, just use startMonitoringSignificantLocationChanges
to update your currentLocation
回答2:
There is no way to monitor more than 20 regions using Apples API.
You have to update the actively monitored regions to the nearest 20 regions.
Whenever you enter/leave a region:
- Check entered location
- Stop monitoring all regions
- Start monitoring the nearest 19 regions (distance to entered location) plus the entered one.
If the result is not satisfying, you might also want to monitor significant location changes to have the chance to update the monitored regions every ~500 meters while not draining too much battery.
来源:https://stackoverflow.com/questions/44946411/how-to-monitor-more-than-20-regions