我想在后端计算地理围栏违规和行驶距离计算。这是我第一次使用谷歌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/2014/03/point-geo-fencing-sample-code.html
用于检查地理围栏多边形:http://stefanbangels.blogspot.be/2013/10/geo-fencing-sample-code.html http://stefanbangels.blogspot.be/2013/10/geo-fencing-sample-code.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)