问题
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 Android. Is there any API specific to regular calculations.
回答1:
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
来源:https://stackoverflow.com/questions/30719757/is-there-any-api-for-calculating-geofence-breach-other-than-android-apis