Javascript 使用 google.maps.geocoder 检索经纬度位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8807141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
retrieving lat/long of location using google.maps.geocoder
提问by Chris Mccabe
i have been trying to use some ajax to save venue location in my application and stumbled across the following code on stack overflow
我一直在尝试使用一些 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;
}
my problem is when i call the function it returns nothing and when i debug and set breakpoints in chrome it breaks on return result first before it breaks on the result[lat] = results[0].geometry.location.Pa;
我的问题是,当我调用该函数时,它不返回任何内容,当我在 chrome 中调试和设置断点时,它首先在返回结果上中断,然后在结果 [lat] = results[0].geometry.location.Pa 上中断;
I know the array should be declared as type array but even when i was just returning the results[0].geometry.location object nothing was being returned
我知道应该将数组声明为数组类型,但即使我只是返回 results[0].geometry.location 对象,也没有返回任何内容
what can i do to return the lat/long of the location so i can store in my db?
我该怎么做才能返回位置的纬度/经度,以便我可以存储在我的数据库中?
采纳答案by Tony Miller
The problem you're facing is that you're treating the geocoder.geocode function as immediately completing before you do the return result. What's really happening is that the geocoder.geocode is triggered, then you get an immediate return of result. Because the asynchronous result has most likely not returned, your result is empty. Think of the geocoding result as a push, not a pull. The storeResult function, not shown, is whatever code you need to do to save the information. Because you're combining a result with an error string, you have to handle that in your storeResult function. As an alternative, you can have a status in the result that indicates succcess or failure.
您面临的问题是,您将 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);
});
}
回答by Lee Smith
This is not the answer but don't use Pa and Qa always use the lng() and lat() functions:
这不是答案,但不要使用 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)}
回答by lakewood
I struggled hard with this and found the following to work.
我为此苦苦挣扎,发现以下方法可行。
Loop through the object and push into a new array:
var newArray = []; for (var key in latLng) { newArray.push(key); }
Call your object with variable values with the square bracket syntax, dot notation breaks:
var lat = latLng[newArray[0]]; var lng = latLng[newArray[1]];
循环遍历对象并推入一个新数组:
var newArray = []; for (var key in latLng) { newArray.push(key); }
使用方括号语法使用变量值调用您的对象,点符号中断:
var lat = latLng[newArray[0]]; var lng = latLng[newArray[1]];
回答by Kamil Krzysztof Dworak
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');
回答by Mr Br
In my case simplest solution for getting latitude and longitude from place object was:
在我的情况下,从地点对象获取纬度和经度的最简单解决方案是:
var latitude=place.geometry.location['k'];
var longitude=place.geometry.location['A'];