Google Maps Search Along Route
Ever been on the road and needed to find a restaurant, restroom, or gas station nearby? The Google Maps Search Along Route API makes it easy to find places of interest right along your route, without having to make significant detours. In this blog post, I'll explain the basics of making a Google Maps Search Along Route API request, and show you how to use it to build a road trip planning app (source code / demo) that suggests cool places to check out along the way.
Part 1: A developer's guide to the Google Routes API
Part 2: Plan a route with multiple stops using the Routes API
Part 3: Using the Google Distance Matrix API for taxi dispatch
Part 4: Google Maps Search Along Route (this article)
Part 5: How to draw a route on Google Maps for Android with the Routes API
How to search along route in Google Maps?
It's easy to find places of interest along your route in the Google Maps app (both web and mobile).
First, enter an origin and destination into the app. In the example below, I used "Vancouver, BC" as my start location and "Kelowna, BC" as my end location. Click the [Search along the route] textbox on the top left of the menu bar.
Next, enter whatever you are looking for into the textbox. I'm feeling a little tired and can use a quick nap, so I search for "rest stops". Google Maps automatically appends a "along the route" to my query.
Third, hit [🔎 Search] and you'll see your search results both on the map and on the listing view on the left hand side.
The Google Maps Search Along Route API allows you to programmatically add this same functionality to your app or website.
What is the Google Maps Search Along Route API?
The Google Maps Search Along Route API enables developers to find places of interest (e.g. gas stations, restaurants, rest stops) that are located along a predefined route. Unlike standard location based searches, this API ensures results are relevant to a driver's actual route, reducing unnecessary detours.
Search Along Route is actually part of the Google Places Text Search API. It uses the encoded polyline generated by the Routes API to bias search results so as to only return places that are conveniently located along the route provided. Let's take a simple example of three restaurants, A, B and C along a highway. Your drive starts from origin and ends at destination.
Calling Text Search with "textQuery" : "Restaurants nearby"
with a locationBias
around your current location will return a list of restaurants nearby. But if you pass in a searchAlongRouteParameters.polyline.encodedPolyline
that matches the route from origin to destination, it will return restaurants A, B and C because they are physically near the route and have minimal detour times from the origin to the destination.
A detour is the additional time required to visit a place compared to the time taken using a direct route from origin to destination. For example, suppose that visiting restaurant C and getting back on the highway takes an extra 5 minutes. If you hadn't made the detour, it would have only taken 3 minutes to get to the destination. The detour duration then is 5 minutes - 3 minutes = 2 minutes additional travel time. The Search Along Route API does its best to return places near the route with minimal detour durations.
Google Maps Search Along Route example
Here's a basic example of how a Search Along Route API call is structured. We'll set "Vancouver, BC" as the origin, "Kelowna, BC" as the destination, and "rest stops" as the text query (you can also play around with the Search Along Route API at https://search-along-route.afi.dev/).
To get started, we first call the Compute Routes endpoint of the Routes API with "Vancouver, BC" as the origin
and "Kelowna, BC" as the destination
. Put routes.polyline
in your field mask so that only the route polyline gets returned.
Google Routes API
Endpoint POST
https://routes.googleapis.com/directions/v2:computeRoutes
Headers
Content-Type: application/json
X-Goog-Api-Key: YOUR_API_KEY
X-Goog-FieldMask: routes.polyline
Request Body
{
"origin": {
"address": "Vancouver, BC"
},
"destination": {
"address": "Kelowna, BC"
},
"travelMode": "DRIVE",
"polylineQuality": "OVERVIEW",
"routingPreference": "TRAFFIC_AWARE",
"departureTime": "2026-02-24T15:00:00Z"
}
Response
{
"routes": [
{
"polyline": {
"encodedPolyline": "ABCqxkHpbnnVwC{ElOiXhD_ ... BcBbCuA|B} "
}
}
]
}
Pass this encoded polyline string in the response to the Text Search API as a searchAlongRouteParameters.polyline.encodedPolyline
like so:
Google Text Search API
Endpoint POST
https://places.googleapis.com/v1/places:searchText
Content-Type: application/json
X-Goog-Api-Key: YOUR_API_KEY
X-Goog-FieldMask: places.displayName,places.formattedAddress,places.location
Request
{
"textQuery": "rest stops",
"searchAlongRouteParameters": {
"polyline": {
"encodedPolyline": "qxkHpbnn ... i@q@[u@Sy@KoAOa@OKk@@WDaKAUEQ[Qk@I}@AeL"
}
}
}
Response
{
"places": [
{
"formattedAddress": "7399 Delta Line Rd, Ferndale, WA 98248, USA",
"location": {
"latitude": 48.9092536,
"longitude": -122.6222666
},
"displayName": {
"text": "Custer-Northbound Rest Area",
"languageCode": "en"
}
},
{
"formattedAddress": "Bow Hill Rest Area, Bow, WA 98232, USA",
"location": {
"latitude": 48.5846413,
"longitude": -122.34704759999998
},
"displayName": {
"text": "Bow Hill Rest Area Southbound",
"languageCode": "en"
}
},
// ... 10 more entries
],
"routingSummaries": [
{
"legs": [
{
"duration": "3849s",
"distanceMeters": 70625
},
{
"duration": "14247s",
"distanceMeters": 360036
}
],
"directionsUri": "https://www.google.com/maps/dir/49.28303,-123.12121/''/49.88632,-119.4966/data=!4m7!4m6!1m0!1m2!1m1!1s0x5485b8d0356424c3:0x70ecef3593f0e432!1m0!3e0"
},
{
"legs": [
{
"duration": "5286s",
"distanceMeters": 112246
},
{
"duration": "15388s",
"distanceMeters": 388667
}
],
"directionsUri": "https://www.google.com/maps/dir/49.28303,-123.12121/''/49.88632,-119.4966/data=!4m7!4m6!1m0!1m2!1m1!1s0x54850ca726ded57f:0x4b994177317941ae!1m0!3e0"
},
// ... 10 more entries
]
}
The Search Along Route API provides a list of places
that match the search query and are located near the specified route. Each place
object has a:
formattedAddress
, a human readable, properly formatted address of the place
.
location
, which gives us the coordinates of the place in latitude
and and longitude
,
displayName
, a convenient human readable name for the place
and,
routingSummaries
, an array of route summaries for each place in the response, including the duration and distance from the origin to the place, as well as from the place to the destination. This information allows us to calculate the total detour duration for each place and filter results to show only those with a detour duration below a specified limit.
Google Maps Search Along Route API location bias
If you use the Search Along Route API with a common search term like "restaurants," you may notice that most results are clustered near the start or end of the route. This happens because the API tends to over index larger cities along the route, which is also where most trips begin and end.
To improve your search results, you can add a locationBias
on the middle portion of the route and ignore the start and end segments. Here one way to do this:
- Decode the route polyline into an array of geographic coordinates, and make note of the 25th and 75th percentile elements. All the coordinates between these two points represent the "middle" 50% of the route.
- Apply the LatLngBounds.extend() method of the Google Maps Geometry Library on each point between the 25th and 75th percentile coordinates. This will determine the "corner" southwest and northeast bounds of the bounding box that contains these points.
- With the bounding box returned in step 2, call the Text Search API and include a
locationBias
object (example shown below). This is a soft constraint that ensures that any place results returned will likely be inside or near the bounding box.rectangle
is a latitude-longitude viewport, represented as two diagonally opposite low and high points. The low point marks the southwest corner of the rectangle, and the high point represents the northeast corner of the rectangle.
{
"locationBias": {
"rectangle": {
"low": {
"latitude": 49.36168000000001,
"longitude": -121.44006000000002
},
"high": {
"latitude": 50.09975000000001,
"longitude": -120.55021
}
}
}
}
- Finally, use the LatLngBounds.getSouthWest() method to retrieve the coordinates for
low
and LatLngBounds.getNorthEast() method forhigh
. And here’s the same Search Along Route request for restaurants along the Vancouver - Kelowna route, but withlocationBias
applied to the middle section.
Ignoring far away places with the Google Maps Search Along Route API
Returning to our original example where we searched for rest stops between Vancouver and Kelowna, you might have noticed that the search results included two rest stops in Washington State. Although kinda along the route, a quick check on Google Maps shows that making a detour to these two rest stops would take almost an hour without traffic.
I'll show you how to filter out places that are far away from the original route, but it's not as easy as it should be. In the routingSummaries
array, you'll find the duration and distance required to detour to that place as a two-leg trip.
The first leg represents the travel duration and distance from the route's starting point to the place (e.g. from the origin to Place A). The second leg represents the travel duration and distance from the place to the final destination (e.g. from Place A to the destination). If you sum the duration of both these legs up and subtract them from the "TRAFFIC_UNAWARE" route duration returned by the Routes API, you can get the detour duration which is the duration difference between the original trip (from the origin to the destination) and the new trip (from the origin to the destination through A).
In the example above, the detour duration for Place A is calculated as 3849s + 14247s - 15278s = 2818s. Once you have the detour duration, you can set a threshold (e.g., 600 seconds or 10 minutes) and filter out any search results that exceed this limit.
Google Maps Search Along Route use cases
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Google Maps Search Along Route pricing
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Building a road trip planning app using the Google Search Along Route API
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
For each place in the response, the search includes the duration and distance required to detour to that place as a two-leg trip:
The first leg contains the travel duration and distance from the route origin to the place. In this example, from the origin to place A.
The second leg contains the travel duration and distance from the place to the route destination. In this example, from A to the destination.
- In this blog post, I'll introduce you to a new api ...
- It combines both the routes API and places API ...
- At the end we'll build a road trip planning app ... that finds places of interest along the way
- How to search along route
- What is the Google Maps Search Along Route API?
- Actually ...
- Takes in a route token produced by the Routes API
- A text string
- Calls the Google Places Text Search API
- Google Maps Search Along Route API example
- Just like text search
- First, add a
- location bias
let you specify a region to bias the search results, the search along route feature in Text Search also lets you bias the search results to include those with minimal detour times from the route origin to the route destination. You can bias search results using either locationBias
or locationRestriction
in combination with the polyline.
Google introduced the Search Along Route feature as part of its Places API enhancements in late 2022. This feature enables developers to find points of interest along a predefined route, enhancing navigation experiences by allowing users to discover relevant stops without significant detours.
Road trip enthusiasts, you’re going to want to try this one out: Explore along your route is a new Google Maps feature that shows you suggestions for interesting local stops along your way to help you save time when planning your drives. So if you’re checking out your journey from point A to point B (and maybe C, D and E) and you want to find a good place for lunch or a scenic view where you can stretch your legs, try this tool. It’ll find hidden gems and local favorites for you without you having to go dig for them on your own.
The Search Along Route API enables developers to find places of interest (e.g., gas stations, restaurants, rest stops) that are located along a predefined route. Unlike standard location-based searches, this API ensures results are relevant to a traveler’s actual path, reducing unnecessary detours.