1

I am currently working with GeoFencing, I able to create GeoFence with the help of CoreLocation framework which is provided by apple. But i am not able to create more than 20 fences when app starting. Here is my code

Thanks in advance

 func createGeofence() {

     if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {



         // region data
         let title = "Verity's"
         let coordinate = CLLocationCoordinate2DMake(17.4220382049842 , 78.3794177044913)
         let regionRadius = 150.0


         // setup region

         let region = CLCircularRegion(center:CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude), radius: regionRadius, identifier: title)

         //Added monitoring
         locationManager.startMonitoring(for: region)  
     }

     else {
     }

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {

       self.enterRegion = region
}


func locationManager(_ manager: CLLocationManager, didExitRegionregion: CLRegion) {

}
Boosa Ramesh
  • 379
  • 3
  • 19
  • Could you explain more detail to help? – Prakash Feb 01 '17 at 05:22
  • sure prakash, i am creating geofence for this location let coordinate = CLLocationCoordinate2DMake(17.4220382049842 , 78.3794177044913) with 100 radious, but i need to create multiple geofences. i hope make you understand – Boosa Ramesh Feb 01 '17 at 05:50
  • You can create 20 goefences for an app. So could check how many geofences are tracking currently. If exceeds 20, then it wont work. What is the error you are getting when creating multiple goefences. – Prakash Feb 01 '17 at 06:00
  • i am not able to create. I mean i dont know how to create – Boosa Ramesh Feb 01 '17 at 06:13
  • Hey @BoosaRamesh please refer my answer i have fixed and its working fine for me. – Vinayak Jan 31 '19 at 14:27
  • https://stackoverflow.com/questions/35105720/swift-geofencing-geolocations-near-user-location/65558490#65558490 – Karan Vakharia Jan 23 '21 at 11:44
  • Please check below link:- https://stackoverflow.com/questions/35105720/swift-geofencing-geolocations-near-user-location/65558490#65558490 – Karan Vakharia Jan 23 '21 at 11:51

2 Answers2

1
var locationManager = CLLocationManager()

@IBAction func didTapOnSaveGeoFences(_ sender: AnyObject) {
    let region = self.region(withGeotification: CLLocationCoordinate2D(latitude: (myLocation?.latitude)!, longitude: (myLocation?.longitide)!), radius: CLLocationDistance(myLocation!.distance),identifier: "\(myLocation?.locationID)", entry: true)
    locationManager.startMonitoring(for: region)
}

func region(withGeotification coordinate: CLLocationCoordinate2D, radius: CLLocationDistance, identifier: String, entry: Bool) -> CLCircularRegion {
    // 1
    let region = CLCircularRegion(center: coordinate, radius: radius, identifier: identifier)
    // 2
    region.notifyOnEntry = entry
    region.notifyOnExit = entry
    return region
}

For more detail: https://www.raywenderlich.com/136165/core-location-geofencing-tutorial

Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55
Prakash
  • 812
  • 6
  • 16
0
    func determineMyCurrentLocation() {

        let locationModel5 = LocationModel()
        locationModel5.name = "locationModel 300"
        locationModel5.lat = 37.33448791
        locationModel5.long = -122.03941089
        locationModel5.radious = 300

        let locationModel4 = LocationModel()
        locationModel4.name = "locationModel 200"
        locationModel4.lat = 37.33448791
        locationModel4.long = -122.03941089
        locationModel4.radious = 200

        let locationModel3 = LocationModel()
        locationModel3.name = "locationModel 150"
        locationModel3.lat = 37.33290406
        locationModel3.long = -122.05391527
        locationModel3.radious = 150

        locationManager = CMCLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.allowsBackgroundLocationUpdates = true

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }

        let ary : [LocationModel] = [locationModel4,locationModel5,locationModel3]

        for mdl in ary {

            let geofenceRegionCenter = CLLocationCoordinate2DMake(mdl.lat, mdl.long);
            let geofenceRegion = CLCircularRegion(center: geofenceRegionCenter, radius: mdl.radious, identifier: mdl.name);
            geofenceRegion.notifyOnExit = true;
            geofenceRegion.notifyOnEntry = true;

            let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
            let mapRegion = MKCoordinateRegion(center: geofenceRegionCenter, span: span)
            self.map.setRegion(mapRegion, animated: true)

            let regionCircle = MKCircle(center: geofenceRegionCenter, radius: mdl.radious)
            self.map.add(regionCircle)

            locationManager.startMonitoring(for: geofenceRegion)
        }
    }


//Location manage delegate method:

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {

            self.showAlert(message: ("\(region.identifier) didEnterRegion"), identifier: ENTERED_REGION_NOTIFICATION_ID)

    }

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {

            self.showAlert(message: ("\(region.identifier) didExitRegion"), identifier: ENTERED_REGION_NOTIFICATION_ID)

    }

//Show alert when entering and exiting

    func showAlert(message: String, identifier: String) {

        let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
        let action = UIAlertAction(title: "OK", style: .destructive, handler: nil)
        alert.addAction(action)
        self.present(alert, animated: true, completion: nil)
    }

// Use this class for temporary

class LocationModel
{
    var name:String!
    var lat:Double!
    var long:Double!
    var radious:Double!
}
Vinayak
  • 329
  • 1
  • 3
  • 10