function getLongLat(address)
{
geocoder = new GClientGeocoder();
if (geocoder) {
geocoder.getLatLng(address,
function(point) {
if (!point) {
return "null";
} else {
return point;
}
}
);
}
}
If I put alert(point) in I get the longitute and latitude.
function getLongLat(address)
{
geocoder = new GClientGeocoder();
if (geocoder) {
geocoder.getLatLng(address,
function(point) {
if (!point) {
return "null";
} else {
return point;
}
}
);
}
}
If I put alert(point) in I get the longitute and latitude.
Share Improve this question asked May 28, 2009 at 10:36 jasonjason4 Answers
Reset to default 4Try this :D
function getLongLat(address)
{
geocoder = new GClientGeocoder();
if (geocoder) {
return geocoder.getLatLng(address,
function(point) {
if (!point) {
return "null";
} else {
return point;
}
}
);
}
return false;
}
It's because your function doesn't have a return statement.
The inner function that is called as a parameter to geocoder.getLatLng
does have a return statement, which is what is confusing you. I suggest you extract the inner function to reduce the confusing aspects.
Try this:
function convert_to_null_if_falsy(value) {
return value || "null";
}
function getLongLat(address) {
geocoder = new GClientGeocoder();
return geocoder.getLatLng(address, convert_to_null_if_falsy);
}
I'm not sure why you want to return the string "null"
instead of undefined
in your callback function, but this code does the same with a lot less hassle.
Keep in mind that getLatLng() is asynchronus: you're passing in an anonymous function for it to execute when it pletes.
Your getLongLat() function won't be able to return point in this case because that anonymous function is running in a pletely different context asynchronously: your function will execute and return before Google's will, and point won't even be in scope for the outer function.
You'll need to call whatever code you want to operate on points inside that anonymous function:
function getLongLat(address)
{
geocoder = new GClientGeocoder();
if (geocoder) {
return geocoder.getLatLng(address,
function(point) {
// point is only in scope for this anonymous inner function
// which is called when your request to Google's service pletes
if (!point) {
// your error condition if a point isn't found
// omgMissingPoint(point);
} else {
// your function to process points here.
// processPoint(point);
}
}
);
}
}
What if geocoder
is not true? And geocoder.getLatLng
doesn’t return any value neither.
So make both return something, perhaps:
function getLongLat(address) {
geocoder = new GClientGeocoder();
if (geocoder) {
return geocoder.getLatLng(address, function(point) {
if (!point) {
return "null";
} else {
return point;
}
});
}
return "null";
}