如何找到two
latitude
和longitude
点之间的距离
对于其中一个纬度和经度点,我将其保存在数据库中&另一个我想使用手机的当前位置
我应该在下面的哪一个中使用它们:
安卓位置计算provider
注意:::任何演示代码示例都会很有用
发布于 2014-03-22 19:42:18
您可以使用内置算法:
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB) ;
更新:(我没有任何错误)
LatLng latLngA = new LatLng(12.3456789,98.7654321);
LatLng latLngB = new LatLng(98.7654321,12.3456789);
Location locationA = new Location("point A");
locationA.setLatitude(latLngA.latitude);
locationA.setLongitude(latLngA.longitude);
Location locationB = new Location("point B");
locationB.setLatitude(latLngB.latitude);
locationB.setLongitude(latLngB.longitude);
double distance = locationA.distanceTo(locationB);
发布于 2014-03-22 19:39:41
您可以使用Haversine算法。另一个用户已经为此编写了一个解决方案:
public double CalculationByDistance(double initialLat, double initialLong,
double finalLat, double finalLong){
int R = 6371; // km
double dLat = toRadians(finalLat-initialLat);
double dLon = toRadians(finalLong-initialLong);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);
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));
return R * c;
}
public double toRadians(deg) {
return deg * (Math.PI/180)
}
https://stackoverflow.com/questions/22577075
复制相似问题