使用google.maps.geocoder获取位置的纬度和经度

18

我一直在尝试使用Ajax在我的应用程序中保存场馆位置,偶然发现了Stack Overflow上的以下代码:

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 中进行调试并设置断点时,在它首先断点到 result[lat] = results[0].geometry.location.Pa; 之前它就已经返回了。我知道数组应该被声明为数组类型,但即使我只返回results[0].geometry.location对象,也没有返回任何内容。

我该如何返回位置的纬度/经度,以便可以存储在我的数据库中?


5个回答

20

这不是答案,但是不要使用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)}

14
为了让这个回答更加清晰:Google地图的地理编码器有时返回的纬度和经度值为Pa和QA,而其他时候则为Sa和Ta...唯一保证可靠的方法是使用geometry.location.lat()和geometry.location.lng()。 - NexusRex

17
你面临的问题是把 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);
    });
}

谢谢这个。我使用JavaScript验证地址,然后将其发布到服务器,并使用HTTP请求获取地理编码信息。可能会在以后进一步重构。 - Chris Mccabe
正如Lee Smith所说,不要使用结果的内部字段。我只是因为OP这样做了才使用它们。Google经常更改内部变量名称,因此您不能依赖它们。请使用API。 - Tony Miller
4
请参考李史密斯的答案,使用geometry.location.lat()和geometry.location.lng()函数代替。 - NexusRex

2
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'); 

你能详细说明一下你的回答吗? - FrankS101
你能否给你的代码加上一些解释说明吗? - Mark

2
我努力地尝试解决这个问题,并发现以下方法有效。
  1. Loop through the object and push into a new array:

    var newArray = [];
    
    for (var key in latLng) {
    
       newArray.push(key);
    
    }
    
  2. Call your object with variable values with the square bracket syntax, dot notation breaks:

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

0
在我的情况下,从地点对象获取纬度和经度的最简单解决方案是:
  var latitude=place.geometry.location['k'];
  var longitude=place.geometry.location['A'];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接