02 May 2010

integrete google route planner in php

Hi friends,

Here you can get how to integrate a route planner to your website. You first need a google map key to your website.

The http://maps.google.com/maps?file=api&v=2&key=abcdefg URL points to the location of the JavaScript file that includes all of the symbols and definitions you need for using the Google Maps API. Your page must contain a script tag pointing to this URL, using the key you received when you signed up for the API. In this example the key is shown as "abcdefg."

Loading the Map

 onload="initialize()" onunload="GUnload()">
The onload attribute is an example of an event handler.
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
The enableDoubleClickZoom() gives the functionality of double click zoom on map. 

The setDirections() takes 3 parameter from, to, locale, . 

The full code here

<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAKkmOXfGaoU8hUkO5hyvNCxTNYfgCZPk3O0B1xtQfj8pKGywwFxQRgtjQcgw2chDs6R0uvZWDB6Sz-g"></script>
<style type="text/css">
body {
font-family: Verdana, Arial, sans serif;
font-size: 11px;
margin: 2px;
table.directions th {

img {
color: #000000;
<script type="text/javascript">

var map;
var gdir;
var geocoder = null;
var addressMarker;

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
gdir = new GDirections(map, document.getElementById("directions"));
GEvent.addListener(gdir, "load", onGDirectionsLoad);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
GEvent.addListener(gdir, "error", handleErrors);

setDirections("Noida", "Agra", "en_US");

function setDirections(fromAddress, toAddress, locale) {
gdir.load("from: " + fromAddress + " to: " + toAddress,
{ "locale": locale });

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

// else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
// alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

else alert("An unknown error occurred.");

function onGDirectionsLoad(){
// Use this function to access information about the latest load()
// results.

// e.g.
// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
// and yada yada yada...


<body onload="initialize()" onunload="GUnload()" style="font-family: Arial;border: 0 none;">
<table width="700">

<td height="20" colspan="2"> </td>

<form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">
<table width="800">
<th align="right"> </th>
<input type="text" size="25" id="fromAddress" name="from" value="Noida"/>
<th align="right">  Search address: </th>
<td align="left"><input type="text" size="25" id="toAddress" name="to" value="Agra" />

<th>Language: </th>
<td colspan="3">
<select id="locale" name="locale">
<option value="en" selected>English</option>
<option value="de">German</option>
<option value="es"> Spanish</option>
<input name="submit" type="submit" value="Get Directions!" />
<table class="directions">
<th> </th>

<td valign="top">
<div id="map_canvas" style="width: 800px; height: 400px"></div>
<td valign="top">
<div id="directions" style="width: 800px"></div>

If you persist any problem, then go here


1 comment:

  1. Hi , how can i modify the code for make a route with multiple address, and not only 2 ?