简体   繁体   English

如何在地图上显示两个地理位置之间的距离?

[英]how to display on map the distance between two geopoints?

我正在开发一个应用程序,用户必须在其中记录位置然后定位汽车。当他选择“定位汽车”选项时,应该显示停放的汽车。我计算了距离,但距离始终显示为0米。任何人都告诉如何在地图上显示人与车之间的距离...我想在地图上显示所停的汽车

you saved the position of the parked car? 您保存了停放的汽车的位置? you've got the actual position of the person? 你有这个人的实际位置吗? so, then you should be able to calculate the difference and get at least the distance (at least of the line in sight)... 因此,那么您应该能够计算出差异并至少获得距离(至少是可见线的距离)...

If you have the position of USER and parked car... 如果您拥有USER和停放的汽车的位置...

then this should work 那么这应该工作

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); 

Use this function which calculates the real distance: 使用此函数来计算实际距离:

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();
}

and use it like this: 并像这样使用它:

double distance = 0;
distance=distFrom(latA,lngA,latB,lngB);

if you want to convert the double to integer use this: 如果要将双精度型转换为整数,请使用以下命令:

int distance=0;
distance=(int) distFrom(latA,lngA,latB,lngB);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM