I want to draw route on map when I place the destination marker from the Places AutoComplete search bar. As soon as I select the Drop location from Auto Complete, I want the route. I have seen some examples of drawing path but can't find any via the Places Autocomplete. My map and Places Autocomplete are working properly. How do I do it?

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), "my_api_ke");
    }


    /*Map Initialisation*/
    mv = findViewById(R.id.Gmapview);
    mv.onCreate(savedInstanceState);
    mv.getMapAsync(this);


    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = lm.getBestProvider(new Criteria(), false);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    checkLocationPermission();
    enableMyLocationIfPermitted();


    //Initialize AutoCompleteSupportFragment

    //Pickup Search Bar
    supportFragment = (AutocompleteSupportFragment)
            getSupportFragmentManager().findFragmentById(R.id.pickup);


    supportFragment1 = (AutocompleteSupportFragment) getSupportFragmentManager()
            .findFragmentById(R.id.drop);

    //Initialize the Fields to get Latitude and Langitude and name of the selected place.
    supportFragment.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG, Place.Field.NAME));
    supportFragment1.setPlaceFields(Arrays.asList(Place.Field.LAT_LNG, Place.Field.NAME));

    //Placing Pickup Marker
    supportFragment.setOnPlaceSelectedListener(newPlaceSelectionListener(){
        @Override
        public void onPlaceSelected(@NonNull Place pickup) {

            Log.i("PickUP", "Place: " + pickup.getName() + ", ");
            String name = pickup.getName();
            LatLng latLng = pickup.getLatLng();

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.title(name);
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            gm.addMarker(markerOptions);

            gm.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));

        }


        @Override
        public void onError(@NonNull Status status) {
            Log.i("pickup", "An error occurred: " + status);

        }
    });

    //Placing Drop Marker
    supportFragment1.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place drop) {
            Log.i("Drop", "Place: " + drop.getName() + ", ");

            String name = drop.getName();
            LatLng latLng = drop.getLatLng();

            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            gm.addMarker(markerOptions);

            gm.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));

        }

        @Override
        public void onError(@NonNull Status status) {
        Log.i("drop", "An error occurred: " + status);

        }
    });
}

2 Answers

0
Amine On Best Solutions

Supposing your LatLng objects are correct and you only need to draw routes, here what you should do

Create a Helper class called DirectionsJSONParser that will later decode your polyline and give you a list of latitude longitude objects.

Here is the link for that class DirectionsJSONParser

For RetrofitClient those are the imports make sure you add them

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; 

Add this to your build.gradle(app) file

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'

Create a retrofit client

//this is a singleton of Retrofit
public class RetrofitClient {
private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {
    if(retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
 //return retrofit object
    return retrofit;
 }
}

Next create a google API interface

public interface IGoogleAPI {

@GET
Call<String> getPath(@Url String url);
}

Create a Common class

public class Common {
   public static String baseURL = "https://maps.googleapis.com/";
   public static IGoogleAPI getGoogleAPI() {
        return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
    }
}

Last but not least , declare a variable in your activity

IGoogleAPI mService;

Initialize that variable in onCreate

mService = Common.getGoogleAPI();

Create a method to draw routes

private void getDirection() {

  String requestAPI;
    try {

        requestAPI = "https://maps.googleapis.com/maps/api/directions/json?" +
                "mode=driving&" +
                "transit_routing_preference=less_driving&" +
                "origin=" + pickupMarker.getLatitude() + "," + pickupMarker.getLongitude() +
                "&destination=" + dropMarker.getLatitude() + "," + dropMarker.getLongitude() + "&" +
                "key=" + getResources().getString(R.string.google_direction_apis);


        mService.getPath(requestAPI)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, retrofit2.Response<String> response) {
                        try {
                            new ParserTask().execute(response.body().toString());

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {

                    }
                });


    } catch (Exception e) {
        e.printStackTrace();
    }


}

Don't forget

  1. To add retrofit dependencies in your build.gradle
  2. To change your API_KEY in the requestString.

It's a long code so if you have any problem tell me .

Call this function getDirection() after you get the drop location.

EDIT

private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {

    ProgressDialog mDialog = new ProgressDialog(NavigationTracking.this);

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog.setMessage("Executing...");
        mDialog.show();
    }

    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... strings) {
        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;
        try {
            jObject = new JSONObject(strings[0]);
            DirectionJsonParser parser = new DirectionJsonParser();
            routes = parser.parse(jObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return routes;
    }

    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> lists) {
        mDialog.dismiss();

        ArrayList points = null;
        PolylineOptions polylineOptions = null;

        Toast.makeText(NavigationTracking.this, "" + lists.size(), Toast.LENGTH_SHORT).show();

        for (int i = 0; i < lists.size(); i++) {

            points = new ArrayList();
            polylineOptions = new PolylineOptions();

            List<HashMap<String, String>> path = lists.get(i);

            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));

                LatLng position = new LatLng(lat, lng);

                points.add(position);

            }

            polylineOptions.addAll(points);
            polylineOptions.width(10);
            polylineOptions.color(Color.RED);
            // the shortest line between two points
            polylineOptions.geodesic(true);
        }

        if (polylineOptions != null) {
            direction = mMap.addPolyline(polylineOptions);
        }


    }
}

Declare

private Polyline direction;

And add this test before calling getDirection()

if (direction != null)
    direction.remove();

EDIT 2

Declare 2 LatLng variables before onCreate() like this :

LatLng pickupLoc;
LatLng dropLoc;

Then when you are picking places your code become like this

//Placing Pickup Marker
supportFragment.setOnPlaceSelectedListener(newPlaceSelectionListener(){
    @Override
    public void onPlaceSelected(@NonNull Place pickup) {

        Log.i("PickUP", "Place: " + pickup.getName() + ", ");
        String name = pickup.getName();
        pickupLoc = pickup.getLatLng();

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(pickupLoc );
        markerOptions.title(name);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        gm.addMarker(markerOptions);

        gm.moveCamera(CameraUpdateFactory.newLatLngZoom(pickupLoc, 11));

    }


    @Override
    public void onError(@NonNull Status status) {
        Log.i("pickup", "An error occurred: " + status);

    }
});


//Placing Drop Marker
supportFragment1.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    @Override
    public void onPlaceSelected(@NonNull Place drop) {
        Log.i("Drop", "Place: " + drop.getName() + ", ");

        String name = drop.getName();
        dropLoc = drop.getLatLng();

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(dropLoc );
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        gm.addMarker(markerOptions);

        gm.moveCamera(CameraUpdateFactory.newLatLngZoom(dropLoc, 11));

        //here you test if your locations aren't null call getDirection() like this

        if (pickupLoc != null && dropLoc != null) {
            if (direction != null)
               direction.remove();

            getDirection();
        }

    }

    @Override
    public void onError(@NonNull Status status) {
    Log.i("drop", "An error occurred: " + status);

    }
});
0
trickster On

So the answer by @Amine is mostly correct but some slight changes are required which I am giving here :

private void getDirection() {

String requestAPI;
try {

    requestAPI = "https://maps.googleapis.com/maps/api/directions/json?" +
            "mode=driving&" +
            "transit_routing_preference=less_driving&" +
            "origin=" + pickuploc.latitude() + "," + 
             pickuploc.longitude() +
            "&destination=" + droploc.latitude() + "," + 
             droploc.longitude() + "&" +
            "key=" + getResources().getString(R.string.google_direction_apis);

             /*If you get "PolylineOptions cannot be null" error and app crashes 
             while trying to draw polyline then try pasting your API 
             key in string directly like this "key=somekey". Though only for testing. */

             mService.getPath(requestAPI)
            .enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, retrofit2.Response<String> 
             response) {
                    try {
                        new ParserTask().execute(response.body().toString());

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {

                }
            });


               } catch (Exception e) {
                 e.printStackTrace();
}


}