更新时间:2023-02-27 08:56:38
停止使用谷歌的Location.distancebetween&安培; location.distanceto功能。他们没有始终如一地工作。
Stop using google's Location.distancebetween & location.distanceto functions. They don't work consistently.
而不是使用直接的公式来计算距离:
Instead use the direct formula to calculate the distance:
double distance_between(Location l1, Location l2)
{
//float results[] = new float[1];
/* Doesn't work. returns inconsistent results
Location.distanceBetween(
l1.getLatitude(),
l1.getLongitude(),
l2.getLatitude(),
l2.getLongitude(),
results);
*/
double lat1=l1.getLatitude();
double lon1=l1.getLongitude();
double lat2=l2.getLatitude();
double lon2=l2.getLongitude();
double R = 6371; // km
double dLat = (lat2-lat1)*Math.PI/180;
double dLon = (lon2-lon1)*Math.PI/180;
lat1 = lat1*Math.PI/180;
lat2 = lat2*Math.PI/180;
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c * 1000;
log_write("dist betn "+
d + " " +
l1.getLatitude()+ " " +
l1.getLongitude() + " " +
l2.getLatitude() + " " +
l2.getLongitude()
);
return d;
}