我正在开发一个应用程序,用户必须记录位置然后定位汽车。当他选择定位汽车选项时,应该显示停放的汽车。我计算距离但距离总是显示 0 米..我不知道为什么?可以有人告诉如何在地图上显示人与停放的汽车之间的距离...我想在地图上显示停放的汽车
问问题
232 次
3 回答
0
如果你有 USER 的位置和停放的汽车......
那么这应该工作
Location locationA = new Location("USER");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("Parked Car");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
于 2012-07-04T14:27:43.430 回答
0
使用此函数计算实际距离:
public static 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));
double dist = earthRadius * c;
int meterConversion = 1609;
return new Double(dist * meterConversion).doubleValue();
}
并像这样使用它:
double distance = 0;
distance=distFrom(latA,lngA,latB,lngB);
如果要将双精度转换为整数,请使用以下命令:
int distance=0;
distance=(int) distFrom(latA,lngA,latB,lngB);
于 2012-07-05T16:46:40.260 回答
0
你保存了停放的汽车的位置?你有这个人的实际位置吗?所以,那么你应该能够计算出差异并至少得到距离(至少是视线)......
于 2012-07-04T14:22:31.827 回答