最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How come this function always returns 'undefined' - Stack Overflow

programmeradmin1浏览0评论
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 jasonjason
Add a ment  | 

4 Answers 4

Reset to default 4

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;
 }

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";
}
发布评论

评论列表(0)

  1. 暂无评论