Search Your Question

Showing posts with label CallAmbulance. Show all posts
Showing posts with label CallAmbulance. Show all posts

How to do geo fencing?

Ans :

Geo-Fence : A Geo-fence is a technology that defines a virtual boundary around a real world geographical area. Every time the user enters or exits the boundary of a certain geofence, actions can be triggered in a location enabled device (often a smartphone). Usually the user will receive a notification with certain information based on its location in real time.

Let's implement : 
In order to work with geofences we need to import CoreLocationframework. After that, we define a locationManager in our viewController and we make the viewController a CLLocationManagerDelegate:


 var locationManager : CLLocationManager = CLLocationManager()


Next, we have to define a region with a center (latitude and longitude) and a radius to monitor. As an example, we are going to define a circular region around the beach in Playa Grande, Mar del Plata with a radius of 400 metres:

 func setUpGeofenceForPlayaGrandeBeach() {
  let geofenceRegionCenter = CLLocationCoordinate2DMake(-38.028308, -57.531508);
  let geofenceRegion = CLCircularRegion(center: geofenceRegionCenter, radius:  400, identifier: "PlayaGrande");
  geofenceRegion.notifyOnExit = true;
  geofenceRegion.notifyOnEntry = true;
  self.locationManager.startMonitoring(for: geofenceRegion)
 }


After creating the region, we are going to set the flags notifyOnExit and notifyOnEntry to true . This will make the CLLocationManagerDelegate trigger a notification each time the user enters or exits the geofenceRegionin the following methods:


func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
  print("Welcome to Playa Grande! If the waves are good, you can try surfing!")
//Good place to schedule a local notification
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Bye! Hope you had a great day at the beach!")
//Good place to schedule a local notification
}




We are still missing the part when the user has to give authorization to the app to track it’s location. So in our viewController we add the following lines to the viewDidLoad method:


 override func viewDidLoad() {
  super.viewDidLoad()
  self.locationManager.requestAlwaysAuthorization()
  self.locationManager.delegate = self
 }

When the user gives authorization to the app, a delegate method is triggered and if the user has given  authorization, we should set up the geofence and start monitoring the region.


 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  if (status == CLAuthorizationStatus.authorizedAlways) {
    self.setUpGeofenceForPlayaGrandeBeach()
  }
 }

As a last step, we need to add a key to the info.plist with the string to be displayed when asking for location authorization:
<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
Usage of geo-fence in Marketing, Reminders, Child Tracking, Vehicle Tracking, Security, Certain area entry and exit.