Android Studio and Google Maps API - Change Travel Mode

Asked

Viewed 238 times

1

Hello guys, beauty? So, I’m developing an application using Android Studio and Google Maps API. So I came across a question that until then I couldn’t find anything that could solve my problem. Here’s the thing, 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 standard "travel mode" to other options like bike, race, train and etc. Someone has already come across this and by some 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);

}
  • How are you getting the route?

  • I am using a method called "urlGoogleDirections" to create a url according to the Source, Destination and Reference points defined on the map. There is a class called Jsonrota that connects to a server and downloads the route according to Google maps.

  • Ask the question this part of the code.

2 answers

1


Oops, good night, guys. I managed to solve my problem as follows: After the line "urlGoogleDirections += "&sensor=false";", I had to add the "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 deals with Travel Mode. I hope I helped. Hug.

My final code went 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);

}

0

There is an option I think so:

travelMode: google.maps.Travelmode[selectedMode].

Where there is selectMode, no matter how you receive it, you place DRIVING, WALKING, BICYCLING or TRANSIT.

But no double quotes if I’m not mistaken.

  • Opa Dávil, actually in the Google Directions url does not have this option you mentioned, but I was able to solve my problem as follows: .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.