Full offline rerouting in the works for Google Maps Navigation?

How to Make an Alternate Route in Google Maps

The first method involves making your own route:

  1. After you enter a location and Google provides a route for you, click anywhere on the blue path to set a point.

  2. 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.

  3. After you complete your alternate route, it locks in.

Getting started

Before using the Directions service in the Maps JavaScript API, first ensure that the Directions API is enabled in the Google Cloud Console, in the same project you set up for the Maps JavaScript API.

To view your list of enabled APIs:

  1. Go to the Google Cloud Console.
  2. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open.
  3. From the list of APIs on the Dashboard, look for Directions API.
  4. If you see the API in the list, you’re all set. If the API is not listed, enable it:
    1. At the top of the page, select ENABLE API to display the Library tab. Alternatively, from the left side menu, select Library.
    2. Search for Directions API, then select it from the results list.
    3. Select ENABLE. When the process finishes, Directions API appears in the list of APIs on the Dashboard.

Video

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.

  1. Enter a destination and starting point.

  2. 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.

  3. 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.

Rendering Directions

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

The DirectionsStatus may return the following values:

  • OK indicates the response contains a valid DirectionsResult.
  • NOT_FOUND indicates at least one of the locations specified in the request’s origin, destination, or waypoints could not be geocoded.
  • ZERO_RESULTS indicates no route could be found between the origin and destination.
  • MAX_WAYPOINTS_EXCEEDED indicates that too many DirectionsWaypoint fields were provided in the DirectionsRequest. See the section below on limits for way points.
  • MAX_ROUTE_LENGTH_EXCEEDED indicates 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_REQUEST indicates that the provided DirectionsRequest was 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_LIMIT indicates the webpage has sent too many requests within the allowed time period.
  • REQUEST_DENIED indicates the webpage is not allowed to use the directions service.
  • UNKNOWN_ERROR indicates 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

The 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:

  1. Create a DirectionsRenderer object.
  2. Call setMap() on the renderer to bind it to the passed map.
  3. Call setDirections() on the renderer, passing it the DirectionsResult as 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 "start" and "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:

View example

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:

View example

A 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.

Textual directions will be provided using the browser’s preferred language setting, or the language specified when loading the API JavaScript using the 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:

View example

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

Tags

Leave a Reply

Your email address will not be published.