Javascript 使用google.maps.geocoder检索位置的lat/long

Javascript 使用google.maps.geocoder检索位置的lat/long,javascript,jquery,google-maps-api-3,geocoding,Javascript,Jquery,Google Maps Api 3,Geocoding,我一直在尝试使用一些ajax在我的应用程序中保存场地位置,无意中发现了以下关于堆栈溢出的代码 function getLatLong(address) { var geocoder = new google.maps.Geocoder(); var result = ""; geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) { if (s

我一直在尝试使用一些ajax在我的应用程序中保存场地位置,无意中发现了以下关于堆栈溢出的代码

function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result[lat] = results[0].geometry.location.Pa;
            result[lng] = results[0].geometry.location.Qa;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    return result;
}
我的问题是,当我调用函数时,它什么也不返回;当我在chrome中调试并设置断点时,它首先在返回结果时中断,然后在结果[lat]=results[0]上中断

我知道数组应该声明为类型数组,但即使在我刚刚返回结果[0]时,也没有返回任何结果


如何返回位置的lat/long以便存储在数据库中?

您面临的问题是,您将geocoder.geocode函数视为在返回结果之前立即完成。真正发生的是触发geocoder.geocode,然后立即返回结果。因为异步结果很可能没有返回,所以结果为空。将地理编码结果看作是一个推,而不是一个拉。storeResult函数(未显示)是保存信息所需的任何代码。因为要将结果与错误字符串组合在一起,所以必须在storeResult函数中处理。或者,您可以在结果中显示一个状态,指示成功或失败

function getLatLong(address) {
var geocoder = new google.maps.Geocoder();
var result = "";
geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
         result[lat] = results[0].geometry.location.Pa;
         result[lng] = results[0].geometry.location.Qa;
     } else {
         result = "Unable to find address: " + status;
     }
     storeResult(result);
    });
}

这不是答案,但不要使用Pa和Qa始终使用lng()和lat()函数:

 place.geometry.location
{...}
    Pa: 56.240477
    Qa: -0.902655999999979
    toString: function(){return"("+this.lat()+", "+this.lng()+")"}
    equals: function(a){return!a?k:Cd(this.lat(),a.lat())&&Cd(this.lng(),a.lng())}
    lat: function(){return this[a]}
    lng: function(){return this[a]}
    toUrlValue: function(a){a=Hd(a)?a:6;return $d(this.lat(),a)+","+$d(this.lng(),a)}

我为此努力奋斗,并发现以下方法可行

  • 在对象中循环并推入新数组:

    var newArray = [];
    
    for (var key in latLng) {
    
       newArray.push(key);
    
    }
    
  • 使用方括号语法、点符号分隔符和变量值调用对象:

    var lat = latLng[newArray[0]];
    
    var lng = latLng[newArray[1]];
    

  • 在我的例子中,从place对象获取纬度和经度的最简单解决方案是:

      var latitude=place.geometry.location['k'];
      var longitude=place.geometry.location['A'];
    

    感谢这一点-我使用javascript验证了地址,然后将其发布到服务器,并使用http请求获取地理代码信息。可能会进一步重构,正如李·史密斯所说,不要使用结果的内部字段。我只是用这些,因为OP这么做了。谷歌经常更改内部变量名,所以你不能依赖它们。使用API。请参阅Lee Smith的答案。。。改为使用geometry.location.lat()和geometry.location.lng()函数。为了更清楚地回答这个问题:谷歌地图的地理编码器有时会将lat和lng值返回为Pa和QA,有时是Sa和Ta。。。唯一有保证的方法是使用geometry.location.lat()和geometry.location.lng()的可能重复项。你能进一步给出你的答案吗?你能对你的代码做一些解释吗?
    function getLatLong(address) 
    {
        var geocoder = new google.maps.Geocoder();
        var result = '';
        geocoder.geocode( { 'address': address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                // Call the lat/lng functions to return a computed value.
                result[lat] = results[0].geometry.location.lat();
                result[lng] = results[0].geometry.location.lng();
            } else {
                result = 'Unable to find address: ' + status;
            }
        });
        return result;
    }
        
    getLatLong('address');