我想计算后端的地理围栏破坏和行驶距离计算.这是我第一次使用谷歌API.我在网上找到的只是Android版.是否有任何特定于常规计算的API.
您可以自己实现它,而无需使用任何框架,它非常容易......
我认为你想要检查你是否在圈内地理围栏中.
要执行此操作,只需计算圆心和您的位置(经度,纬度)之间的距离.如果距离小于您的圆半径,那么您就在地理围栏内,否则您就在地理围栏之外.
像这样:
boolean checkInside(Circle circle, double longitude, double latitude) {
return calculateDistance(
circle.getLongitude(), circle.getLatitude(), longitude, latitude
) < circle.getRadius();}
Run Code Online (Sandbox Code Playgroud)
要计算两点之间的距离,您可以使用:
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);
}
Run Code Online (Sandbox Code Playgroud)
这个公式叫做Haversine公式.它考虑到了地球的曲线.结果以米为单位.
我也在我的博客上描述过它:
用于检查地理围栏圈(它还描述了两点之间的距离计算):http: //stefanbangels.blogspot.be/2014/03/point-geo-fencing-sample-code.html
检查地理围栏多边形:http://stefanbangels.blogspot.be/2013/10/geo-fencing-sample-code.html
| 归档时间: |
|
| 查看次数: |
3815 次 |
| 最近记录: |