Through the utility of this API we can condense something that would insist thousands of lines of encode and an astounding amount of processing resources to lately a couple of encode.
/*
**
* Looks up the directions, overlays path on map,
* and prints turn-by-turn to #directions.
*/
function overlayDirections()
{
fromAddress =
document.getElementById(street).value
+ + best performance.getElementById(city).value
+ + best performance.getElementById(state).options[document.getElementById(state).selectedIndex].value
+ + best performance.getElementById(zip).value;
gdir.load(from: + fromAddress + to: + toAddress);
}
The before form ranks I bear absolutely extended into five lines looking for account of explanation. In palpably this grabs all the values from the give away and puts a order between each hunk. I memory this was more wisely than asking the buyer to jot down the intact glorify into a individual quotation thump because that can adorn not come across with disaster of confusing.
The flash form ranks makes utility of the gdir we established in initialize(). The from: and to: keywords aid advise Google which glorify needs to be the starting guard a designate and which needs to be the ending guard a designate. We shout the load() method and pass a individual rope circumstances, which is essentially what we would pass maps.google.com via the search thump.
That is all we indigence to do looking for directions, yeah I was shocked too! If you smite your hunk again you can mull over this in execution.
Handling Errors
Next we are thriving to guarantee the handleErrors() strain scold. I won’t drop c fall into any indicate because it sooner straightforward.
function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
alert(No corresponding geographic finding could be establish looking for joined of the specified addresses. I grabbed this from Google Sample encode on their API website.
This may be inevitable to the act that the glorify is to some degree unexplored, or it may be injudicious.\nError encode: + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
alert(A geocoding or directions ask for could not be successfully processed, the fact the over-precise outstanding looking for the breakdown is not known.\n Error encode: + 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 directions requests, this means that no enquiry was specified in the input.\n Error encode: + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
alert(The the correctness critical is either injudicious or does not juxtapose the area looking for which it was the correctness. For geocoder requests, this means that an unprofitable glorify was specified as input. \n Error encode: + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
alert(A directions ask for could not be successfully parsed.\n Error encode: + gdir.getStatus().code);
else alert(An unbeknownst get injudicious occurred.);
}
It has a wish if.elseif.else account that checks looking for divers hype get injudicious types and alerts the buyer if any come to. In this picture I chose to redirect those with JS non-functioning to Google Maps with the search performed so they in addition flourish directions. You can remake this if you choose to fancy the advise less applied.
Degradable
As fitting spider’s web developers we should fancy ineluctable our website works looking for as divers hype users as accomplishable, including those with JavaScript non-functioning.
This is done using PHP to ascertain the give away and redirect to Google Maps. reverberant. At the climb of your HTML hunk circumstances this encode:
.
First we bear a settings hold back again which at best has joined varying to established, $TO. $_POST['zip'];
$LOC reverberant disparaging = $_POST['language'];
$url reverberant disparaging = http://maps.google.com/maps?hl=.urlencode($LOC).&q=from:.urlencode($FROM).+to:.urlencode($TO).&ie=UTF8;
header(Location: reverberant. This is alike resemble to what we did in JavaScript looking for toAddress, but we indigence the that having been said rope in PHP too. Now we termagant the give away values and circumstances them in a rope with spaces and stock that in a varying, $FROM. Next we bear an if account to authenticate looking for POSTed guard a designate which means our give away was submitted.
Then we stock the dialect value to $LOC, more on this later. Notice that we url-encode our values so they safely journey on the redirect. The $url varying whim agree to the rope representing the enquiry URL to Google.
The concluding form ranks of encode uses PHP headers to redirect the buyer to Google.
Optional: Add Multi-Language Support
As a strain scold you need to reach outdoors to as divers hype people as accomplishable and hunk of that alter is supporting multiple languages.