Search code examples
androidgsongoogle-maps-android-api-2retrofit

Google Map Direction Api using Retrofit


I want to draw the route between two Location. I used the retrofit library to call the API. I didn't get any response. I need the polyline in an ArrayList. How i do that? Need help to create the GsonAdapter also... Thank you..


Solution

  • In the activity `

         String base_url = "http://maps.googleapis.com/";
    
        Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();
    
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(base_url)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();
    
        MyApiRequestInterface reqinterface = restAdapter.create(MyApiRequestInterface.class);
    
        reqinterface.getJson(fromPosition.latitude + "," + fromPosition.longitude, toPosition.latitude + "," + toPosition.longitude, new Callback<DirectionResults>() {
    
    
            @Override
            public void success(DirectionResults directionResults, Response response) {
                Log.i("zacharia", "inside on success" +directionResults.getRoutes().size());
                ArrayList<LatLng> routelist = new ArrayList<LatLng>();
                if(directionResults.getRoutes().size()>0){
                    ArrayList<LatLng> decodelist;
                    Route routeA = directionResults.getRoutes().get(0);
                    Log.i("zacharia", "Legs length : "+routeA.getLegs().size());
                    if(routeA.getLegs().size()>0){
                        List<Steps> steps= routeA.getLegs().get(0).getSteps();
                        Log.i("zacharia","Steps size :"+steps.size());
                        Steps step;
                        Location location;
                        String polyline;
                        for(int i=0 ; i<steps.size();i++){
                            step = steps.get(i);
                            location =step.getStart_location();
                            routelist.add(new LatLng(location.getLat(), location.getLng()));
                            Log.i("zacharia", "Start Location :" + location.getLat() + ", " + location.getLng());
                            polyline = step.getPolyline().getPoints();
                            decodelist = RouteDecode.decodePoly(polyline);
                            routelist.addAll(decodelist);
                            location =step.getEnd_location();
                            routelist.add(new LatLng(location.getLat() ,location.getLng()));
                            Log.i("zacharia","End Location :"+location.getLat() +", "+location.getLng());
                        }
                    }
                }
                Log.i("zacharia","routelist size : "+routelist.size());
                if(routelist.size()>0){
                    PolylineOptions rectLine = new PolylineOptions().width(10).color(
                            Color.RED);
    
                    for (int i = 0; i < routelist.size(); i++) {
                        rectLine.add(routelist.get(i));
                    }
                    // Adding route on the map
                    mMap.addPolyline(rectLine);
                    markerOptions.position(toPosition);
                    markerOptions.draggable(true);
                    mMap.addMarker(markerOptions);
                }
            }
    
            @Override
            public void failure(RetrofitError retrofitError) {
                System.out.println("Failure, retrofitError" + retrofitError);
            }
        });`
    

    Create retrofit interface

    public interface MyApiRequestInterface {
    
    @GET("/maps/api/directions/json")
    public void getJson(@Query("origin") String origin,@Query("destination") String destination, Callback<DirectionResults> callback);}
    

    Create Model classes

    public class DirectionResults {
    @SerializedName("routes")
    private List<Route> routes;
    
    public List<Route> getRoutes() {
        return routes;
    }}
     public class Route {
    @SerializedName("overview_polyline")
    private OverviewPolyLine overviewPolyLine;
    
    private List<Legs> legs;
    
    public OverviewPolyLine getOverviewPolyLine() {
        return overviewPolyLine;
    }
    
    public List<Legs> getLegs() {
        return legs;
    }
    }
    
    public class Legs {
    private List<Steps> steps;
    
    public List<Steps> getSteps() {
        return steps;
    }
    }
    
    public class Steps {
    private Location start_location;
    private Location end_location;
    private OverviewPolyLine polyline;
    
    public Location getStart_location() {
        return start_location;
    }
    
    public Location getEnd_location() {
        return end_location;
    }
    
    public OverviewPolyLine getPolyline() {
        return polyline;
    }
    }
    
    public class OverviewPolyLine {
    
    @SerializedName("points")
    public String points;
    
    public String getPoints() {
        return points;
    }
    }
    
    public class Location {
    private double lat;
    private double lng;
    
    public double getLat() {
        return lat;
    }
    
    public double getLng() {
        return lng;
    }
    }
    

    Create the Polyline Decode method

    public class RouteDecode {
    
    public static ArrayList<LatLng> decodePoly(String encoded) {
        ArrayList<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;
        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;
            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;
    
            LatLng position = new LatLng((double) lat / 1E5, (double) lng / 1E5);
            poly.add(position);
        }
        return poly;
    }
    }