Building a Traffic-Aware Route Finder with Google Maps Routes API

We are going to dive into building a dynamic route finder using the powerful Google Maps Routes API. Creating an application that lets users input their starting point and destination, choose their preferred mode of transport (car or two-wheeler), and instantly receive the fastest route.

But here’s where it gets interesting: Our route finder will go beyond just directions. We’ll leverage real-time traffic data to highlight congested areas along the route, marking them in yellow and red for easy identification. This allows users to anticipate delays and make informed decisions to optimize their journey.

What is an API?

An API, or Application Programming Interface, is a set of rules and protocols that define how apps communicate with each other over the internet. They define where an app should request data from, how to send that request, and the format in which data will be returned.

What is the Routes API?

The Routes API generates driving, walking, biking, or public transit directions between multiple locations. These directions take into account real-time traffic conditions as well as the type of vehicle being used.

With more accurate ETAs (Estimated Time of Arrival), flexible routing options that include two-wheeler (motorbike) routes, faster response times and polylines color-coded according to real-time traffic, it’s a huge upgrade over its predecessor, the Directions API.

How does the Routes API work?

To get the best route from a start point to an end point (including any waypoints along the way), we’ll be using the ComputeRoutes endpoint of the Routes API. ComputeRoutes needs five pieces of information to return a route.

destination: The GPS coordinates of your end location:


{
    "destination": {
        "location": {
            "latLng": {
                "latitude": 1.3425023,
                "longitude": 103.9571411
            }
        }
    }
}

Note: If you don’t have the GPS coordinates of your origin and destination, you’ll need to first convert your origin and destination addresses to latitude and longitude using the Google Maps Geocoding API.

travelMode: One of “DRIVE” (car), “WALK” (on foot), “TRANSIT” (public transport), “BICYCLE” (bicycle or personal mobility device with preference for cycling paths) or “TWO_WHEELER” (motorcycle).:

{
    "travelMode": "DRIVE"
}

routingPreference: One of three choices: “TRAFFIC_UNAWARE,” “TRAFFIC_AWARE,” and “TRAFFIC_AWARE_OPTIMAL.” Selecting “TRAFFIC_AWARE” returns a route that takes into account real-time traffic conditions.

{
    "routingPreference": "TRAFFIC_AWARE"
}

And finally,

departureTime: Allows you to specify the start time of the route. When used together with “routingPreference”: “TRAFFIC_AWARE”, it returns travel times that take into account real time traffic.

{
    "departureTime": "2023-10-15T15:01:23.045123456Z"
}

In the response, take note of the travelAdvisories object which indicates which part of the route is affected by traffic congestion.


{
    "travelAdvisory": {
        "speedReadingIntervals": [
            {
                "startPolylinePointIndex": 0,
                "endPolylinePointIndex": 24,
                "speed": "NORMAL"
            },
            {
                "startPolylinePointIndex": 24,
                "endPolylinePointIndex": 25,
                "speed": "SLOW"
            },
			... // 5 more entries
        ]
    }
}

The tuple {“startPolylinePointIndex”: 24, “endPolylinePointIndex”: 25, “speed”: “SLOW”} indicates that the segment of the route between points 24 and 25 is experiencing slow traffic. This information can be used to visually represent traffic congestion by coloring this segment of the route yellow, as demonstrated in the demo application.

How much does the Routes API cost?

There are three pricing tiers for the new Routes API – Basic ($5 CPM), Advanced ($10 CPM) and Preferred ($15 CPM).

Basic: This version of the Routes API is identical to the old Directions API. It returns a route, polyline and ETA.

Advanced: The advanced tier allows users to specify intermediate waypoints on the route, each with a heading or specified side of the road, and includes real-time traffic calculations.

Preferred: This highest tier lets users turn on two-wheeler routing, traffic on polylines and comes with improved speed and performance enhancements.

The tier used is automatically calculated based on the features requested on the API call e.g. setting up “routingPreference”: “TRAFFIC_AWARE” to enable traffic on polylines immediately bumps up your API call to the Preferred Tier.

Conclusion

In this blog post, we explained how to use the new Google Maps Routes API in the context of a route finder application that finds the fastest route between two addresses. The route displayed used the new traffic aware option of the Routes API to indicate road segments with congestion so that the user could be kept informed of not only when he would arrive at his destination, but also the traffic he will experience on the way.

Subscribe Our Newsletter

No Spam, only provides you the latest technology updates.

Follow Us

Leave Us Your Message
We are ready to talk!

Leave Us Your Message.
We are ready to talk!

Leave Us Your Message.
We are ready to talk!

Can't Find What You Need? Join Our Latest Event!

Be the first to learn about
New Trends