Almost There, Google Ajax Api Problem

35 views
Skip to first unread message

Sami

unread,
Dec 27, 2008, 7:31:38 AM12/27/08
to Google AJAX APIs
Well here is what i set out to do.

Basically have users to enter a Start and an End Location.

this is then calculaated to get local searches and then put through
the direction function

So in short:

function setDirections(fromAddress, toAddress) {

gFirstSearch.execute(fromAddress);
first = gFirstSearch.results[0];

gSecondSearch.execute(toAddress);
second = gSecondSearch.results[0];

gdir.load("from: " + (first.lat + ", " + first.lng) + " to: " +
(second.lat + ", " + second.lng));

//gdir.load("from: " + (first.lat + ", " + first.lng) +" to:
London");

return false;
}

All works fine andit calculates perfectly. Exept get this i Have to
click the Get Direction Button Twice for it to work.

I figured out its the execute command that is doing it. Assoon as i
take those out the directions are calculated in one click

look here for demo: http://connectairport.co.uk/testmap.php

And yes i have return false. By this time i have but return false
every where it is posible just to see if its a stacking issue. Please
Look at the full of my Below Code for further understanding:

//<![CDATA[

google.load('search', '1');
google.load('maps', '2');

var map;
var gdir;
var gFirstSearch;
var gSecondSearch;
var first;
var second;

function OnLoad(){

if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(52.037366,-0.703726), 12);
gdir = new GDirections(map);
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);

}

gFirstSearch = new google.search.LocalSearch();
gFirstSearch.setCenterPoint(map);
//gFirstSearch.setSearchCompleteCallback(null, FirstSearch);

gSecondSearch = new google.search.LocalSearch();
gSecondSearch.setCenterPoint(map);
//gSecondSearch.setSearchCompleteCallback(null, SecondSearch);


}

function setDirections(fromAddress, toAddress) {

gFirstSearch.execute(fromAddress);
first = gFirstSearch.results[0];

gSecondSearch.execute(toAddress);
second = gSecondSearch.results[0];

gdir.load("from: " + (first.lat + ", " + first.lng) + " to: " +
(second.lat + ", " + second.lng));

//gdir.load("from: " + (first.lat + ", " + first.lng) +" to:
London");

return false;
}

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(){

document.getElementById("title").innerHTML = gdir.getDistance().html;
//document.getElementById("from").value = fromAddString;
// and yada yada yada...
}



google.setOnLoadCallback(OnLoad, true);

//]]>

Ben Lisbakken

unread,
Dec 27, 2008, 6:10:02 PM12/27/08
to Google-AJAX...@googlegroups.com
Hey Sami --

It looks like you've taken down the demo on that page you linked to.  Could you put it back up so that I can try to see what is happening?

Thanks,
Ben

Sami

unread,
Dec 28, 2008, 9:50:11 AM12/28/08
to Google AJAX APIs
http://connectairport.co.uk/quote.php

I figured out the problem, i shall mention how i fixed it.

First of all when you use "google.search.LocalSearch.execute
(fromAddress);" it runs as a thread, Which is fine if you are trying
to return one address

But if you want two different searches to be ran in the Gdirection.load
("From: address, To: address"), the problem here is that the first
search returns the value where as the second is still retriving the
result. Therefore when you do Gdirection.load it returns an error.

Therefore on the second click the second returns as well and the
Gdirection.load works.

A way to solve it is that run the whole process using the
setSearchCompleteCallback(null, functionName),

setSearchCompleteCallback(null, functionName), says that functionName
will be ran after the completion of the LocalSearch.

This way you wait for the first one to return before running the
second and then when the second one finishes you then run the
Gdirection.load(..) function.

Below is the snippet of the code if it helps you.

<script type='text/javascript'>
google.load('search', '1');
google.load('maps', '2');

//Define our Globals
var map; //Map API
var gdir; //Direction API
var gFirstSearch; //The From Local Search
var gSecondSearch; //The To Local Search

var fromAddress; //From Address The user inputs
var toAddress; //To Address the user inputs

var first; //First Set of Results for From Search
var second; //Second Set of Results for To Search

//On Load, Load all the Details Needed
function OnLoad(){

//Set up the Map and the Globals
//If the Browser Supports its
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(52.037366,-0.703726), 7);
map.removeMapType(G_HYBRID_MAP);

var mapControl = new GMapTypeControl();
map.addControl(mapControl);
map.addControl(new GLargeMapControl());

gdir = new GDirections(map);
GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);

gFirstSearch = new google.search.LocalSearch();
gFirstSearch.setCenterPoint(map);
gFirstSearch.setSearchCompleteCallback(null, FirstSearch);

gSecondSearch = new google.search.LocalSearch();
gSecondSearch.setCenterPoint(map);
gSecondSearch.setSearchCompleteCallback(null, SecondSearch);
}
}

//Run the From Search
//Runs after the gFirstSearch.execute has finished
//Reference: setSearchCompleteCallback
function FirstSearch(){

if (!gFirstSearch.results.length){ alert("Could Not Find: " +
fromAddress + "\n Please Try Refining your 'From' Address Field");
return; }
//Return the First Result into a Variable
first = gFirstSearch.results[0];

//Execute the Second
gSecondSearch.execute(toAddress);

}

//Run the To Search
//Runs after the gSecondSearch.execute has finished
//Reference: setSearchCompleteCallback
function SecondSearch(){

if (!gSecondSearch.results.length){ alert("Could Not Find: " +
toAddress + "\n Please Try Refining your 'To' Address Field");
return; }

//Returns the Second results into a Variable
second = gSecondSearch.results[0];

//Plot our Graph
gdir.load("from: " + (first.lat + ", " + first.lng) + " to: " +
(second.lat + ", " + second.lng));

}

//Use to Execite our Form Commands
function setDirections(ifromAddress, itoAddress) {

//Initiate the inputs into our Global Variables
fromAddress = ifromAddress;
toAddress = itoAddress;

//Execute our Search
gFirstSearch.execute(fromAddress);

//Return False so our broweser dosent Refresh
return false;
}

//Set the Values in our HTML after Direction has loaded
function onGDirectionsLoad(){

var miles = gdir.getDistance().meters * 0.000621371192; //Convert to
Miles
document.getElementById("distance").innerHTML = "Distance: " + miles
+ " ml";

// and yada yada yada...
}
</script>

Use the setDirections(fromAddress, toAddress) in your form to get the
thing running.

If you have similar problems or wana know more about this code please
email me :). I like helping cause others help me
Reply all
Reply to author
Forward
0 new messages