directions snippets

How to draw driving directions on a Google map (API V3)

Tagged driving, directions, google-maps  Languages javascript

This snippet gives you the driving directions from the center of the map to Dubai:

var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();

var request = {
  origin:  map.getCenter(), 
  destination: 'Dubai',
  travelMode: google.maps.DirectionsTravelMode.DRIVING

directionsService.route(request, function(result, status) {
  if (status == google.maps.DirectionsStatus.OK) {
     // Display the distance:
     $('#distance').html(result.routes[0].legs[0].distance.value + " meters");
     // Display the duration:
     $('#duration').html(result.routes[0].legs[0].duration.value + " seconds");

How to embed Google Maps driving directions on your site

Tagged google-maps, driving, directions  Languages html
<form action="" method="get">
  <label for="saddr">Where are you starting your trip?</label>
  <input type="text" name="saddr" id="saddr" value="Adelaide, Australia" />
  <input type="submit" value="Go" />
  <input type="hidden" name="daddr" value="Melbourne, Australia" />
  <input type="hidden" name="hl" value="en" />