Search code examples
iosswift3geolocationmonitoringcllocationmanager

Can a CLLocationManager manage 2 or more locations at the same time in swift 3


i have a problem that i cant seem to find the answer anywhere, i have a global CLLocationManager() variable, in the viewController, so that i can see when the user enters or leave a location with the locationManager callBacks, and it works perfectly with only one location at a time, but i want to make the app monitor 2 or more locations at the same time. I created a function to start it:

  var coreLocationManger = CLLocationManager()

  func setMonitoredRegion(location:CLLocation) {

    let startLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let monitoredRegion = CLCircularRegion(center: startLocation, radius: 100, identifier: "Local Region")

        coreLocationManger.startMonitoring(for: monitoredRegion)
        coreLocationManger.allowsBackgroundLocationUpdates = true

        coreLocationManger.delegate = self
        monitoredRegion.notifyOnEntry = true
        monitoredRegion.notifyOnExit = true

}

this func, asks for a location to be managed, and when i use this multiple times with various locations, it just manages the last location that was given! Do you guys have any tips on that? Thank you


Solution

  • You keep overwriting the region each time you call the function. Make sure region identifier is different for each location.