Javascript值不分配

时间:2016-06-07 08:46:39

标签: javascript

我在这里分配了地点ID" placeid = results [0] .place_id"但它没有。它返回我未定义。分配操作在那里失败。

function match_up_dn(address)
{
    geocoder = new google.maps.Geocoder();
    var placeid;

    if (geocoder) 
    {
        geocoder.geocode({
            'address': address
        }, function (results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
               console.log("al:-"+results[0].place_id);
                placeid = results[0].place_id;
            }
            else
            {
                alert("undefined address");
            }
        });
    }

    return placeid;
}

1 个答案:

答案 0 :(得分:0)

试试这个

function match_up_dn(address)
{
    geocoder = new google.maps.Geocoder();
    var placeid;

        geocoder.geocode({
            'address': address
        }, function (results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
               console.log("al:-"+results[0].place_id);
               placeid = results[0].place_id;
               return placeid;
            }
            else
            {
                alert("undefined address");
            }
        });

}