Is there any API for calculating Geofence breach other than Android API's

前端 未结 1 2018
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 17:03

I would like to calculate geofence breach and driving distance calculation in the backend. This is my first time using the google API. All I find in the web is are for Andro

1条回答
  •  执笔经年
    2021-01-07 17:50

    You can implement it yourself, without using any frameworks, it's very easy...

    I presume that you want to check if you're inside a circle geofence or not.

    To do this, just calculate the distance between the center of the circle and your location (longitude, latitude). If the distance is smaller than your circle radius, then you're inside the geofence, otherwise you're outside the geofence.

    Like this:

        boolean checkInside(Circle circle, double longitude, double latitude) {
            return calculateDistance(
                circle.getLongitude(), circle.getLatitude(), longitude, latitude
            ) < circle.getRadius();}
    

    To calculate the distance between two points, you can use this:

    double calculateDistance(
      double longitude1, double latitude1, 
      double longitude2, double latitude2) {
        double c = 
            Math.sin(Math.toRadians(latitude1)) *
            Math.sin(Math.toRadians(latitude2)) +
                Math.cos(Math.toRadians(latitude1)) *
                Math.cos(Math.toRadians(latitude2)) *
                Math.cos(Math.toRadians(longitude2) - 
                    Math.toRadians(longitude1));
        c = c > 0 ? Math.min(1, c) : Math.max(-1, c);
        return 3959 * 1.609 * 1000 * Math.acos(c);
    }
    

    This formula is called the Haversine formula. It takes into account the earths curvation. The results are in meters.

    I've also described it on my blog:

    • for checking geofencing circles (it also describes distance-calculation between two points): http://stefanbangels.blogspot.be/2014/03/point-geo-fencing-sample-code.html

    • for checking geofencing polygons: http://stefanbangels.blogspot.be/2013/10/geo-fencing-sample-code.html

    0 讨论(0)
提交回复
热议问题