Search code examples
iosswiftcllocationgeofencinggoogle-maps-sdk-ios

Run action when iOS device is near some map point


I have the mobile app that uses user's location when the app is running to show some places on a map (now it is Google Map).

I want to add a new feature - App in the background should notify the user when the device is near some map point or entering the predefined area.

  1. How can it be done? Are there different approaches?
  2. Where to start from?
  3. What if my points stored on the server - should I save them on the device first and what if can't do it and new map points can be added when the app is not in memory?
  4. What will be the most challenging in creating such feature?

p.s. I know about Ray's geofencing tutorial but I want to know - are there any different approaches and also about pitfalls of such feature


Solution

  • First of all you must enable location updates and remote notifications in background modes(Your Target->Capabilities), then you must write in your location update function some code to detect that user is close to some location exapmle:

       func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
                if let userLocation = locations.first {
                    if location.coordinate.langtitude <= <some value> || 
                       location.coordinate.langtitude >= <some value> || ... {
                          runAction()
                        }
                  }
        }
    
        func runAction() {
          //show local notification here
        }