views:

974

answers:

4
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.

+1  A: 

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";
}
Gumbo
`return "null"` returns the string "null". Write `return null` instead.
Török Gábor
Maybe that’s what he wants. Take a look at his original code. He did also use the string `"null"` instead of the `null` value.
Gumbo
+4  A: 

Try 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;
 }
SleepyCod
sarnath'd - you might want to explain that he's missing a return, and why this matters rather than letting the OP diff it though :)
annakata
Nope, I must be missing something. If I have an alert above return point I get the cordinates. Am I caling it wrong? alert(getLongLat(address));
+2  A: 

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.

Magnar
+1 goddamn just when I was going to post a similar explanation :-)
Rytmis
+1  A: 

Keep in mind that getLatLng() is asynchronus: you're passing in an anonymous function for it to execute when it completes.

Your getLongLat() function won't be able to return point in this case because that anonymous function is running in a completely 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 completes
           if (!point) {
              // your error condition if a point isn't found
              // omgMissingPoint(point);
           } else {          
              // your function to process points here.
              // processPoint(point);
           }
         }
       );
    }

}

ajm
I think this is the problem. However using this method still yields the same result.