Hey, in this post, i will tell you about
Directions feature in Google Maps Javascript v3 and give you a small example. First, you must be familiar with
Geocoding. You can read my post about Geocoding
here. After you understand about Geocoding, let's start the show...!!! ^_^
Directions is the capability of Google Maps for displaying the route and directions between two location. There are three things that you must specify before you send a directions request to Google Maps.
- Origin = specify the starting point.
- Destination = specify the end point
- TravelMode = specify what mode of transport to use. You must specify it because it will give different route for different transport. Until this post launched, there are three type of transports that were supported by Google Maps, (TravelMode.DRIVING, TravelMode.WALKING, TravelMode.BICYCLING).
There are other parameter, you can find them
here. For my example below, i will only use the three parameters above. You will also need two object for Directions features. They are :
- DirectionsService that will calculate the route for you with its route() method.
- DirectionsDisplay that will render the routes for you.
Okay, that's all we need to make a simple directions application. For the example, i will create a web application that will let you give a start and end point in the textbox, calculate the route and display it for you and also give the detailed text-based directions for you (like "Turn right onto bla bla bla..").
First, don't forget to add the Google Maps Javascript's script reference like below, inside the
<head> tag before you use any part of Google maps.