【问题标题】:Find the distance between two locations in android? [duplicate]在android中找到两个位置之间的距离? [复制]
【发布时间】:2012-11-03 05:55:54
【问题描述】:

可能重复:
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";

我得到了地名作为字符串我想用这两个字符串计算两个地方之间的距离我怎样才能实现它 提前致谢。

【问题讨论】:

  • 我以Haversine公式see here为例。
  • 在发布新问题之前尝试在 stackoverflow 上搜索!
  • 不,我得到了两个地方,我想根据这些地方找到距离?

标签: android google-maps distance


【解决方案1】:

我认为这段代码对你有用:

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 更好
【解决方案2】:

要找到距离试试这个只是通过起点和终点返回你的距离

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

【讨论】:

  • autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View view, int pos, long id) { startplace=adapter.getItem(pos); System.out.println("=========START PLACE==========="+startplace); //onPlaceSelected(adapter.getItem(pos)); } });
  • 由此我得到了开始和结束的地方?
  • 如何根据这两个地方找到距离?
  • @user1787493 在两个不同的变量中获取您的地理点,然后将该地理点传递给此函数。
  • 如何在自动完成的文本视图中获取所选地点的纬度和纬度位置?
猜你喜欢
  • 2023-03-02
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-27
相关资源
最近更新 更多