Calculate distance between two points in google maps V3

后端 未结 15 1702
醉梦人生
醉梦人生 2020-11-22 02:27

How do you calculate the distance between two markers in Google maps V3? (Similar to the distanceFrom function inV2.)

Thanks..

15条回答
  •  不思量自难忘°
    2020-11-22 03:05

    To calculate distance on Google Maps, you can use Directions API. That will be one of the easiest way to do it. To get data from Google Server, you can use Retrofit or Volley. Both has their own advantage. Take a look at following code where I have used retrofit to implement it:

    private void build_retrofit_and_get_response(String type) {
    
        String url = "https://maps.googleapis.com/maps/";
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        RetrofitMaps service = retrofit.create(RetrofitMaps.class);
    
        Call call = service.getDistanceDuration("metric", origin.latitude + "," + origin.longitude,dest.latitude + "," + dest.longitude, type);
    
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Response response, Retrofit retrofit) {
    
                try {
                    //Remove previous line from map
                    if (line != null) {
                        line.remove();
                    }
                    // This loop will go through all the results and add marker on each location.
                    for (int i = 0; i < response.body().getRoutes().size(); i++) {
                        String distance = response.body().getRoutes().get(i).getLegs().get(i).getDistance().getText();
                        String time = response.body().getRoutes().get(i).getLegs().get(i).getDuration().getText();
                        ShowDistanceDuration.setText("Distance:" + distance + ", Duration:" + time);
                        String encodedString = response.body().getRoutes().get(0).getOverviewPolyline().getPoints();
                        List list = decodePoly(encodedString);
                        line = mMap.addPolyline(new PolylineOptions()
                                        .addAll(list)
                                        .width(20)
                                        .color(Color.RED)
                                        .geodesic(true)
                        );
                    }
                } catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
                }
            }
    
            @Override
            public void onFailure(Throwable t) {
                Log.d("onFailure", t.toString());
            }
        });
    
    }
    

    Above is the code of function build_retrofit_and_get_response for calculating distance. Below is corresponding Retrofit Interface:

    package com.androidtutorialpoint.googlemapsdistancecalculator;
    
    
    import com.androidtutorialpoint.googlemapsdistancecalculator.POJO.Example;
    
    import retrofit.Call;
    import retrofit.http.GET;
    import retrofit.http.Query;
    
    public interface RetrofitMaps {
    
    
    /*
     * Retrofit get annotation with our URL
     * And our method that will return us details of student.
     */
    @GET("api/directions/json?key=AIzaSyC22GfkHu9FdgT9SwdCWMwKX1a4aohGifM")
    Call getDistanceDuration(@Query("units") String units, @Query("origin") String origin, @Query("destination") String destination, @Query("mode") String mode);
    

    }

    I hope this explains your query. All the best :)

    Source: Google Maps Distance Calculator

提交回复
热议问题