计算同一起始目的地和终点之间的距离



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

目前,方向 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();

您可以使用 Google 地图 API 实用工具库中的SphericalUtil.computeLength来计算List<LatLng>的长度

你有没有尝试过使用 android.location.Location ?有一个函数 distanceTo(Location),用于计算两个位置之间的距离。

最新更新