从Jquery函数返回数据

从Jquery函数返回数据,jquery,google-maps-api-3,Jquery,Google Maps Api 3,我试图从地理反向编码中获取地址组件,并返回实际的呼叫者。但是,即使地址获取正确,返回值也始终未定义。这就是我试图获取地址的方式 var geocoder; var addresss = codeLatLng(23.750875259244058, 56822900823215); alert(address); //Getting the address through reverse geo coding var address; function codeLatLng(lat, lng)

我试图从地理反向编码中获取地址组件,并返回实际的呼叫者。但是,即使地址获取正确,返回值也始终未定义。这就是我试图获取地址的方式

var geocoder;
var addresss = codeLatLng(23.750875259244058, 56822900823215);
alert(address);

//Getting the address through reverse geo coding
var address;

function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({
        'latLng': latlng
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                //formatted address
                alert(results[0].formatted_address)

                //find country name
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < results[0].address_components[i].types.length; b++) {

                        //there are different types that might hold a city    
                        admin_area_lvl_1 usually does in come cases looking
                        for sublocality type will
                        be more appropriate
                        if (results[0].address_components[i].types[b] ==
                            "administrative_area_level_1") {
                            //this is the object you are looking for
                            city = results[0].address_components[i];
                            break;
                        }
                    }
                }
                //city data
                alert(city.short_name + " " + city.long_name)
                address = city.short_name;

            } else {
                alert("No results found");
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
    return address;
}
var地理编码器;
var地址=codeLatLng(23.75087525924405856822900823215);
警报(地址);
//通过反向地理编码获取地址
var地址;
功能代码LATLNG(lat,lng){
var latlng=新的google.maps.latlng(lat,lng);
地理编码({
“latLng”:latLng
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果)
如果(结果[1]){
//格式化地址
警报(结果[0]。格式化的\u地址)
//查找国家名称
对于(var i=0;i
您必须使用jQuery.Deferred(),因为这是一个异步函数

 var geocoder;
 var addresss = codeLatLng(this.getPosition().lat(),      
 this.getPosition().lng());

 // now your addresss wariable is a promise you can access it like this

addresss.done(function(data){
    //data is your resolved addres
    //do your staff here
    //Getting the address through reverse geo coding
    var address = data;
})


function codeLatLng(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);

    //define jquery deffered
    var promise= new jQuery.Deferred();

    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                //formatted address
                console.log(results[0].formatted_address)

                //find country name
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < 
 results[0].address_components[i].types.length; b++) {

                        //there are different types that might hold a city    
   admin_area_lvl_1 usually does in come cases looking for sublocality type will 
   be more appropriate
                        if (results[0].address_components[i].types[b] == 
    "administrative_area_level_1") {
                            //this is the object you are looking for
                            city = results[0].address_components[i];
                            break;
                        }
                    }
                }
                //city data
                console.log(city.short_name + " " + city.long_name)
                 promise.resolve( city.short_name);

            } else {
                console.log("No results found");
            }
        } else {
            console.log("Geocoder failed due to: " + status);
        }
    });
    return promise;

}
var地理编码器;
var addresss=codeLatLng(this.getPosition().lat(),
这个.getPosition().lng());
//现在你的地址是一个承诺,你可以像这样访问它
地址完成(功能(数据){
//数据是您解析的地址
//这里有你的员工吗
//通过反向地理编码获取地址
var地址=数据;
})
功能代码LATLNG(lat,lng){
var latlng=新的google.maps.latlng(lat,lng);
//定义不同的jquery
var promise=newjquery.Deferred();
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果)
如果(结果[1]){
//格式化地址
console.log(结果[0]。格式化的\u地址)
//查找国家名称
对于(var i=0;i
选项2您可以使用如下回调函数对其进行调用

 var geocoder;
 var addresss = 
 codeLatLng(this.getPosition().lat(), this.getPosition().lng(), function(address){
    //do your staff here
    //Getting the address through reverse geo coding
 });


function codeLatLng(lat, lng, callback) {
    var latlng = new google.maps.LatLng(lat, lng);

    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results)
            if (results[1]) {
                //formatted address
                console.log(results[0].formatted_address)

                //find country name
                for (var i = 0; i < results[0].address_components.length; i++) {
                    for (var b = 0; b < 
 results[0].address_components[i].types.length; b++) {

                        //there are different types that might hold a city    
   admin_area_lvl_1 usually does in come cases looking for sublocality type will 
   be more appropriate
                        if (results[0].address_components[i].types[b] == 
    "administrative_area_level_1") {
                            //this is the object you are looking for
                            city = results[0].address_components[i];
                            break;
                        }
                    }
                }
                //city data
                console.log(city.short_name + " " + city.long_name)
                //callback your address here
                callback( city.short_name);

            } else {
                console.log("No results found");
            }
        } else {
            console.log("Geocoder failed due to: " + status);
        }
    });
    callback(false);

}
var地理编码器;
变量地址=
codeLatLng(this.getPosition().lat()、this.getPosition().lng()、函数(地址){
//这里有你的员工吗
//通过反向地理编码获取地址
});
函数代码LATLNG(lat、lng、回调){
var latlng=新的google.maps.latlng(lat,lng);
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果)
如果(结果[1]){
//格式化地址
console.log(结果[0]。格式化的\u地址)
//查找国家名称
对于(var i=0;i
geocoder.geocode()
函数是异步的,带有一个回调函数,该函数在收到谷歌的响应后运行。但在您的情况下,
返回地址语句在回调函数之外,因此
codeL