5

I'm developing an application where user can set multiple locations. I get succeed to show notifications when user get Enter or Leave specific region using GeoFencing.

Now, there is situation that i need to provide monitoring for all saved locations and it can be hundreds and more. I've read at the given link "You can have multiple active geofences, with a limit of 100 per device user."

Is there any way add more then 100 geofences per device user ?

Thanks!

6
  • If the documentation is saying that only 100 items can be added how come there is any way to Add any more them ? Apr 16, 2015 at 9:25
  • Although you can reduce fence by not creating fences of same place of enter n exit type. I hope you know you can give both types in geofence for same location Apr 16, 2015 at 9:26
  • Thanks for reply... but in some situation we have more then 100 geofences at that movement of time app features not worked as per app requirement.
    – Naitik
    Apr 16, 2015 at 9:30
  • you need to reduce the geofence, there is no other way. and remember 100 geofences are allowed not PER APP but PER DEVICE. Apr 16, 2015 at 9:32

1 Answer 1

23
  1. Have an ArrayList with all the geofences you want to monitor +100.
  2. Listen to location updates.
  3. When you get a location update, if the ArrayList of all your geofences is more than 100, remove all geofences been monitored and then calculate the 100 nearest geofences using the harvesine formula:

    public static final double R = 6372.8; // In kilometers
    
    public static double haversine(double lat1, double lon1, double lat2, double lon2) {
      double dLat = Math.toRadians(lat2 - lat1);
      double dLon = Math.toRadians(lon2 - lon1);
      lat1 = Math.toRadians(lat1);
      lat2 = Math.toRadians(lat2);
    
      double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
      double c = 2 * Math.asin(Math.sqrt(a));
      return R * c;
    }
    

    That will give you the distance between the two locations. After that you could compare that distance with the geofence region radius to know if is inside the region.

Note: This distance will be in kilometers if your radius is on meters then just multiply the haversine method result with 1000 so that it's converted to meters.

Reference

  1. Start monitoring the result list of the 100 nearest geofences.

This will allow you to always monitor the 100 nearest geofences based on your location. Been able to monitor more than 100 since it will change the monitoring geofence regions always to the 100 nearest geofence regions.

2
  • 2
    Instead of using Haversine you can also use the built in function, Location.distanceBetween(), which uses the WGS84 ellipsoid algorithm.
    – skeeve
    Mar 21, 2018 at 3:00
  • Geofences are stateful (they are transitions not locations), strongly recommend against a naive approach of uninstalling all and then installing them again.
    – GDanger
    Mar 15, 2021 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.