我从互联网某处借鉴了下面的方法(不记得是从哪里借来的)。但它只是一个简单的过程,用于查找两个 GPS 点之间的距离。它工作得很好,但由于我要在数百万个点之间运行它,所以可能有点慢。
我想知道是否有人知道一种计算成本更低的方法。
精度需要在“正确”的大致范围内,但不需要 100% 准确。
private double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
return earthRadius * c;
}
}
顺便说一句,我确实找到了许多其他相关的问题,但它们并没有真正关注我的速度问题。