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!
}