Content of the material
How to Make an Alternate Route in Google Maps
The first method involves making your own route:
After you enter a location and Google provides a route for you, click anywhere on the blue path to set a point.
Drag that point to a new location to change the route. When you do this, other suggested alternate routes disappear from the map, and the driving directions change.
The estimated drive time and distance change as you adjust the route, which is helpful if you're trying to stay within a certain time frame. Monitor these changes as you make an alternative route and adjust your plan accordingly.
Google Maps automatically "sticks" the new path on the road for you, so you don't need to worry that it's sending you through forests or neighborhoods that you can't drive in. The path it gives is a legitimate way to get to the destination.
After you complete your alternate route, it locks in.
To view your list of enabled APIs:
- Go to the Google Cloud Console.
- From the list of APIs on the Dashboard, look for Directions API.
- If you see the API in the list, you’re all set. If the API is not listed, enable it:
- At the top of the page, select ENABLE API to display the Library tab. Alternatively, from the left side menu, select Library.
- Search for Directions API, then select it from the results list.
- Select ENABLE. When the process finishes, Directions API appears in the list of APIs on the Dashboard.
How to Add Multiple Destinations to a Route
Another way to plan an alternate route on Google Maps is to add multiple destinations to a suggested route.
Enter a destination and starting point.
Click or tap the + button underneath the destination you entered to open a third field where you can input an additional destination or click on the map to enter the new destination.
Repeat the process to add additional destinations.
To change the order of the stops, click and hold the menu to the left of one of the destinations and then drag it up or down the list.
Fine-tuning the routes that Google Maps offers is possible through the Options button in the route panel. Use it to avoid highways, tolls, and ferries.
Depending on the route you choose, it may experience heavy traffic or delays, in which case you can select an alternate route to reach your destination faster. Turn on live traffic indicators in Google Maps with the three-lined stacked menu at the upper-left corner of the page.
If you're using the mobile app, change the route options using the menu in the upper-right corner of the app. Toggling live traffic on and off is available through the layers button hovering over the map.
Initiating a directions request to the
DirectionsService with the
route() method requires passing a callback which executes upon completion of the service request. This callback will return a
DirectionsResult and a
DirectionsStatus code in the response.
Status of Directions Query
DirectionsStatus may return the following values:
OKindicates the response contains a valid
NOT_FOUNDindicates at least one of the locations specified in the request’s origin, destination, or waypoints could not be geocoded.
ZERO_RESULTSindicates no route could be found between the origin and destination.
MAX_WAYPOINTS_EXCEEDEDindicates that too many
DirectionsWaypointfields were provided in the
DirectionsRequest. See the section below on limits for way points.
MAX_ROUTE_LENGTH_EXCEEDEDindicates the requested route is too long and cannot be processed. This error occurs when more complex directions are returned. Try reducing the number of waypoints, turns, or instructions.
INVALID_REQUESTindicates that the provided
DirectionsRequestwas invalid. The most common causes of this error code are requests that are missing either an origin or destination, or a transit request that includes waypoints.
OVER_QUERY_LIMITindicates the webpage has sent too many requests within the allowed time period.
REQUEST_DENIEDindicates the webpage is not allowed to use the directions service.
UNKNOWN_ERRORindicates a directions request could not be processed due to a server error. The request may succeed if you try again.
You should ensure that the directions query returned valid results by checking this value before processing the result.
Displaying the DirectionsResult
DirectionsResult contains the result of the directions query, which you may either handle yourself, or pass to a
DirectionsRenderer object, which can automatically handle displaying the result on a map.
To display a
DirectionsResult using a
DirectionsRenderer, you need to do the following:
- Create a
setMap()on the renderer to bind it to the passed map.
setDirections()on the renderer, passing it the
DirectionsResultas noted above. Because the renderer is an
MVCObject, it will automatically detect any changes to its properties and update the map when its associated directions have changed.
The following example calculates directions between two locations on Route 66, where the origin and destination are set by the given
"end" values in the dropdown lists. The
DirectionsRenderer handles display of the polyline between the indicated locations, and the placement of markers at the origin, destination, and any waypoints, if applicable.
In the HTML body:
The following example shows directions using different modes of travel between the Haight-Ashbury to Ocean Beach in San Francisco, CA:
In the HTML body:
DirectionsRenderer not only handles display of the polyline and any associated markers, but also can handle the textual display of directions as a series of steps. To do so, call
setPanel() on your
DirectionsRenderer, passing it the
<div> in which to display this information. Doing so also ensures that you display the appropriate copyright information, and any warnings which may be associated with the result.
language parameter. (For more information, see Localization.) In the case of transit directions, the time will be displayed in the time zone at that transit stop.
The following example is identical to that shown above, but includes a
<div> panel in which to display directions:
In the HTML body:
New Introducing Filters for Stack Overflow
Filters aren’t just for selfies anymore. Introducing Filters for Stack Overflow – the best way to beautify the site where you spend 10 hours a day.Learn more about Filters