我想在后端计算地理围栏突破和行驶距离计算。这是我第一次使用谷歌API。我在网上找到的都是Android版的。有没有专门针对常规计算的API。
最佳答案
你可以自己实现,无需使用任何框架,非常简单...
我认为您想检查自己是否在圆形地理围栏内。
为此,只需计算圆心与您所在位置(经度、纬度)之间的距离即可。如果距离小于您的圆半径,则您在地理围栏内,否则您在地理围栏外。
像这样:
boolean checkInside(Circle circle, double longitude, double latitude) {
return calculateDistance(
circle.getLongitude(), circle.getLatitude(), longitude, latitude
) < circle.getRadius();}
要计算两点之间的距离,可以使用:
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);
}
这个公式称为半正矢公式。 它考虑了地球曲率。 结果以米为单位。
我也在我的博客上描述过它:
用于检查地理围栏圈(它还描述了两点之间的距离计算): http://stefanbangels.blogspot.be/2014/03/point-geo-fencing-sample-code.html
用于检查地理围栏多边形: http://stefanbangels.blogspot.be/2013/10/geo-fencing-sample-code.html
关于java - 除了 Android API 之外,是否有任何用于计算地理围栏违规的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30719757/