0

可能重复:
从我的位置到android中目标位置的距离计算

我对 android 谷歌地图很陌生,我想显示两个地方之间的距离。为此,我编写了两个 autocompletedtextview,我得到了 from 和 to Locations。通过使用

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

我将地名作为字符串我想使用这两个字符串位置计算两个地方之间的距离我该如何实现它在此先感谢。

4

2 回答 2

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)
于 2012-11-03T06:01:34.837 回答
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;
        }
于 2012-11-03T05:58:29.290 回答