【问题标题】:Calculate distance between same start destination and end destination计算同一起点和终点之间的距离
【发布时间】:2016-05-31 16:14:03
【问题描述】:

如何从编码的折线中获取多个纬度和经度之间的距离。

目前,Direction API 计算起点和终点之间的距离。但我也希望能够计算起点目的地是同一位置时的距离。

 // Generates an encoded string with all the latitudes and longitues
 // used to get the exact distance, otherwise the google will calculate the closet path.
    String encodedLatLngList = PolyUtil.encode(latLngListTemp);
    String encodedWaypointString = "waypoints=enc:" + encodedLatLngList;

       RESTClient RESTClient = new RESTClient();
       Call<Direction> call = RESTClient.getGoogleApiService().getDirectionWithEncodedWaypoints(originLatLng, destinationLatLng, encodedWaypointString);
       call.enqueue(new Callback<Direction>() {

          @Override
          public void onResponse(Call<Direction> call, Response<Direction> response) {

          if (response.isSuccessful()) {

                 Direction direction = response.body();
             if (direction.getRoutes().size() > 0) {
                 String distance = direction.getRoutes().get(0).getLegs().get(0).getDistance().getText();
                 String locationA = direction.getRoutes().get(0).getLegs().get(0).getStartAddress();
                 String locationB = direction.getRoutes().get(0).getLegs().get(0).getEndAddress();
                 String duration = direction.getRoutes().get(0).getLegs().get(0).getDuration().getText();

【问题讨论】:

    标签: java android google-maps google-directions-api


    【解决方案1】:

    您可以使用Google Maps API Utility Library 中的SphericalUtil.computeLength 来计算List&lt;LatLng&gt; 的长度

    【讨论】:

      【解决方案2】:

      您是否尝试过使用 android.location.Location ?有一个函数 distanceTo(Location) 可以计算两个位置之间的距离。

      【讨论】:

      • 我知道如何计算两点之间的距离,但我需要计算起点和终点位置相同的多个点之间的距离。
      • 那你为什么不能逐对计算并将所有结果相加?
      猜你喜欢
      • 1970-01-01
      • 2016-09-23
      • 2014-11-29
      • 2014-02-28
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多