flutterdartgoogle-cloud-platformgoogle-routes-api

Using Routes Api not Direction Api Flutter


How should we send an HTTP request to the routes API, not the direction API, to get more precise polylines in Flutter? It would be helpful if anyone could give me an example of sending that request to the routes API.

I tried searching in the hope of finding detailed documentation or something and an example of how to do it..but I could not. I used directions API, which was not so precise.

What I want is that the routes API accepts and HTTP post request of the from "https://routes.googleapis.com/directions/v2:computeRoutes" but I cant understand how to send it...all I am asking is to give me an example on how this should be done


Solution

  • The HTTP post request is as follows:

    HTTP route request using curl:

    curl -X POST -d '{
      "origin":{
        "location":{
          "latLng":{
            "latitude": 37.419734,
            "longitude": -122.0827784
          }
        }
      },
      "destination":{
        "location":{
          "latLng":{
            "latitude": 37.417670,
            "longitude": -122.079595
          }
        }
      },
      "travelMode": "DRIVE",
      "routingPreference": "TRAFFIC_AWARE",
      "departureTime": "2023-10-15T15:01:23.045123456Z",
      "computeAlternativeRoutes": false,
      "routeModifiers": {
        "avoidTolls": false,
        "avoidHighways": false,
        "avoidFerries": false
      },
      "languageCode": "en-US",
      "units": "IMPERIAL"
    }' \
    -H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \
    -H 'X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline' \
    'https://routes.googleapis.com/directions/v2:computeRoutes'
    

    HTTP route request using Postman:

    Url: https://routes.googleapis.com/directions/v2:computeRoutes?key=YOUR_API_KEY

    Make sure to add X-Goog-FieldMask in headers with its values. If you have no clue what to put as values just put * or

    routes.duration, routes.distanceMeters, routes.polyline.encodedPolyline
    

    which is a basic header call using the google routes api

    and finally the body in your post request will be somewhat like this:

    {
      "origin":{
        "location":{
          "latLng":{
            "latitude": 37.419734,
            "longitude": -122.0827784
          }
        }
      },
      "destination":{
        "location":{
          "latLng":{
            "latitude": 37.417670,
            "longitude": -122.079595
          }
        }
      },
      "travelMode": "DRIVE",
      "routingPreference": "TRAFFIC_AWARE",
      "departureTime": "2023-10-15T15:01:23.045123456Z",
      "computeAlternativeRoutes": false,
      "routeModifiers": {
        "avoidTolls": false,
        "avoidHighways": false,
        "avoidFerries": false
      },
      "languageCode": "en-US",
      "units": "IMPERIAL"
    }
    

    Make sure to check the official documentation too!

    https://developers.google.com/maps/documentation/routes/compute_route_directions

    https://developers.google.com/maps/documentation/routes