找到android中两个位置之间的距离?

时间:2012-11-03 05:55:54

标签: android google-maps distance

  

可能重复:
  Distance calculation from my location to destination location in android

我对Android谷歌地图非常新,我希望显示两个地方之间的距离。因为我写了两个autocompletedtextview我得到了来自和使用

的地点。
public String host = "https://maps.googleapis.com";
public String searchPath = "/maps/api/place/autocomplete/json";
public String detailsPath = "/maps/api/place/details/json";

我把地名作为字符串我想用两个字符串来计算两个地方之间的距离我怎样才能实现它     在此先感谢。

2 个答案:

答案 0 :(得分:7)

我认为此代码对您有用:

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)

答案 1 :(得分:0)

找到距离尝试这只是通过开始和结束点返回距离

 double distance;
 GeoPoint StartP;
 GeoPoint EndP;

    distance=CalculationByDistance(StartP,EndP);

    public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
            double Radius = 6371;
            double lat1 = StartP.getLatitudeE6() / 1E6;
            double lat2 = EndP.getLatitudeE6() / 1E6;
            double lon1 = StartP.getLongitudeE6() / 1E6;
            double lon2 = EndP.getLongitudeE6() / 1E6;
            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 = Radius * c;
            temp=temp*0.621;
            return temp;
        }