Website Babble Webmaster Forums  
  #1 (permalink)  
Old 01-12-2008, 03:16 PM
Regular Babbler
 
Join Date: Jan 2008
Location: San Diego, CA
Posts: 45
Send a message via AIM to Levi
Default Embedded Google Driving Directions & Map

What up everyone -

Since everyone has been so kind in responding with positive feedback on my first post, I thought I'd contribute to the forum...

I've seen many sites that in my opinion take the easy way out and provide a simple link to google maps for things such as driving directions. Why not embed the driving directions directly into your site? Well for those that don't know how this is done, here's step by step instructions:

If you're not sure what I'm referring to, see an example here: http://www.faithtab7.org/website2007/map.html

Instructions:
1. Be sure to sign up with google for an API key. You can do so here:
http://code.google.com/apis/maps/signup.html

2. Place the following code between your opening and closing <head> tags. Be sure to modify the sections in red to your desired settings:

<script src=" http://maps.google.com/?file=api&amp;v=2.x&amp;key=ABQIAAAA7YD0976wC-Td9sBOEMLcphRVfyNKMsYfpJvTpLAvrGOoU8-IZhRoNm3CQkHEMmwk9shinwP5HixBbA"
type="text/javascript"></script>
<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);
GEvent.addListener(gdir, "error", handleErrors);

setDirections("San Diego", "3666 Madison Ave. San Diego, CA 92116", "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...
}
</script>


3. Place the following code inside your <body> tag. The finished body tag should look like this:
<body onload="initialize()" onunload="GUnload()">

4. Place the following code in the body of your page (between the opening and closing <body> tags). Be sure to change sections in red to your desired settings:

<form action="#" onsubmit="setDirections(this.from.value, this.to.value, this.locale.value); return false">
<table>

<tr><th align="right">From:&nbsp;</th>

<td><input type="text" size="27" id="fromAddress" name="from"
value="San Diego"/></td>
<th align="right">&nbsp;&nbsp;To:&nbsp;</th>
<td align="right"><input type="text" size="27" id="toAddress" name="to"
value="3666 Madison Ave. San Diego, CA 92116" /></td></tr>

<tr><th>Language:&nbsp;</th>
<td colspan="3"><select id="locale" name="locale">

<option value="en" selected>English</option>
<option value="fr">French</option>

<option value="de">German</option>
<option value="ja">Japanese</option>
<option value="es">Spanish</option>
</select>

<input name="submit" type="submit" value="Get Directions!" />
</td></tr>
</table>


</form>

<br/>
<table>
<tr><th>Formatted Directions</th><th>Map</th></tr>

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

</tr>
</table>


That should do it! Let me know if anyone has any questions.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -6. The time now is 05:46 PM.



WB Sponsors

Profit Lance Review

affiliate network

Work At Home Jobs

Expert PPC Coaching



 Subscribe to the Website Babble Feeds

2 Create a Website Homepage | 2 Create a Website Blog


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0