如何获得公里的距离在机器人?
问题描述:
我很新的谷歌地图我要计算的android两地之间的距离。
I am very new to Google maps I want calculate the distance between two places in android.
有关,我得到的,我写了下面的code两地纬度和滞后的位置:
For that I get the two places lat and lag positions for that I write the following code:
private double getDistance(double lat1, double lat2, double lon1, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double temp = 6371 * c;
temp=temp*0.621;
return temp;
}
以上code不能给两地之间的精确距离。什么是另一种方式来发现的距离,请给我任何建议。
The above code can't give the accurate distance between two places. What is the another way to find distance please give me any suggestions.
答
使用下面这个code你发现的距离,但要在千米转换。
Using following this code you find distance but you want to convert in kilometer.
double distance;
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);