How to monitor more than 20 regions?

Viewed 1840

I'm working on an app in which there are 66 annotations. These annotations are centers of regions and whenever user enters a region, a notification appears, but that works only for first 20 of them because there's a limited number on monitoring regoins. My problem is that I don't know how to monitor more than 20 regions. Could anyone help?

3 Answers

Short and clean solution:

private var locations = [CLLocation]()

private var currentLocation: CLLocation? {
    didSet {
        evaluateClosestRegions()
    }
}

private func distance(from location: CLLocation) -> Double {
    return currentLocation.distance(from: location))
}

private func evaluateClosestRegions() {
    locationManager.monitoredRegions.forEach {
        locationManager.stopMonitoring(for: $0)
    }

    locations.sort {
        distance(from: $0) < distance(from: $1)
    }

    locations.prefix(20).forEach {
        ...
    }
}
Related