简体   繁体   中英

Not able to set region for monitoring using CLCircular Region in swift

I try to set a CLCircularRegion in didUpdateLocations method using the code

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    NSLog("Location Updated from did Update Locations")
    let persist = Persistence()
    let currentLocation : CLLocation = locations[0]
    let latitude : Double = currentLocation.coordinate.latitude
    let Longitude : Double = currentLocation.coordinate.longitude
    let regionID = "GeoFenceTrack"
    let region : CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(persist.getObject(mdmiosagent_Constants.LOCATIONRADIUS))!, identifier: regionID)

    NSLog("the center of the region is \(region.center) and the redius of the region is \(region.radius)")
    self.sendLocation(currentLocation)
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    locationManager.delegate = self
    locationManager.startMonitoringForRegion(region)
}

error occurs on the line

locationManager.startMonitoringForRegion(region) 

and the error that occurs is

********* iPad *******[4018] <Warning>: Failed for region Error Domain=kCLErrorDomain Code=5 "(null)"

the coordinates and the radius is correctly set to the region . I am not monitoring 20+ regions as well . I am monitoring only one region . Can anyone suggest me where am I going wrong ? Thank you in advance .

Its working. You might have set a wrong radius.

This code snippet is working for me:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    NSLog("Location Updated from did Update Locations")
    let currentLocation : CLLocation = locations[0]
    let latitude : Double = currentLocation.coordinate.latitude
    let Longitude : Double = currentLocation.coordinate.longitude
    let regionID = "GeoFenceTrack"
    let region : CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(1000), identifier: regionID)

    NSLog("the center of the region is \(region.center) and the redius of the region is \(region.radius)")
    //self.sendLocation(currentLocation)
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
    locationManager.delegate = self
    locationManager.startMonitoringForRegion(region)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM