Android Studio and Google Maps API - Change Travel Mode

1

Oops, guys, what's up? So I'm developing an app using Android Studio and the Google Maps API. So I came across an issue that until then I could not find anything that could solve my problem. Here's how I'm working with Routes and by default Google maps uses the "Driving" route when creating JSON to mount the route in the app. I would like to change this default travel mode to other options like bike, race, train and so on. Has anyone ever come across this and by chance managed to solve?

This is my class:

private void criarUrlGoogleDirections(){

    urlGoogleDirections = "http://maps.googleapis.com/maps/api/directions/json?origin=" +
            latLngPontoPartida.latitude + "," + latLngPontoPartida.longitude +
            "&destination=" + latLngPontoFinal.latitude + "," + latLngPontoFinal.longitude +
            "&waypoints=";

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

        urlGoogleDirections += objListaLatLngWaypoints.get(i).latitude
                + "," + objListaLatLngWaypoints.get(i).longitude;

        if ((i+1) < objListaLatLngWaypoints.size()) {

            urlGoogleDirections += "|";
        }
    }

    urlGoogleDirections += "&sensor=false";

    Log.i("URL", urlGoogleDirections);

}
    
asked by anonymous 15.12.2017 / 17:02

2 answers

1

Oops, good night guys. I was able to solve my problem in the following way: After the line "urlGoogleDirections +=" & sensor = false ";", I had to add "urlGoogleDirections +=" & = mode = bicycling ";" to mount routes using Bicycles as a means of transport. The Type can be changed to any other available on Google Maps. Just take a look at the Google Api documentation where it is about Travel Mode. Hope this helps. Thanks.

My final code looks like this:

private void criarUrlGoogleDirections(){

    urlGoogleDirections = "http://maps.googleapis.com/maps/api/directions/json?origin=" +
            latLngPontoPartida.latitude + "," + latLngPontoPartida.longitude +
            "&destination=" + latLngPontoFinal.latitude + "," + latLngPontoFinal.longitude +
            "&waypoints=";

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

        urlGoogleDirections += objListaLatLngWaypoints.get(i).latitude
                + "," + objListaLatLngWaypoints.get(i).longitude;

        if ((i+1) < objListaLatLngWaypoints.size()) {

            urlGoogleDirections += "|";
        }
    }

    urlGoogleDirections += "&sensor=false";
    urlGoogleDirections += "&mode=bicycling";

    Log.i("URL", urlGoogleDirections);

}
    
19.12.2017 / 03:18
0

There is an option I think so:

travelMode: google.maps.TravelMode [selectedMode].

Where you have selectMode, whatever you get, you put DRIVING, WALKING, BICYCLING or TRANSIT.

But no double quotation mark if you do not fool me.

    
15.12.2017 / 17:30