谷歌地图地理编码 API V3 未在 javascript 函数中返回结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132685/
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
Google maps geocode API V3 not returning result in javascript function
提问by Jason
I'm trying to use the Google geocoder API V3 to plot a location on a map based on an address specified by the user, code is below.
我正在尝试使用 Google geocoder API V3 根据用户指定的地址在地图上绘制位置,代码如下。
When I make a request directly (e.g. to http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=false) I get the expected response. However, when I make the same request using the code below, the midpointvariable is always undefined after the getLatLongfunction has exited.
当我直接提出请求(例如http://maps.googleapis.com/maps/api/geocode/json?address=peterborough&sensor=false)时,我得到了预期的响应。但是,当我使用下面的代码发出相同的请求时,在getLatLong函数退出后,中点变量始终未定义。
What am I doing incorrectly?
我做错了什么?
function loadFromSearch(address)
{
midpoint = getLatLong(address);
mapCentre = midpoint;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}
function getLatLong(address)
{
var result;
var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
$.getJSON(url,
function (data){
if (data.status == "OK")
{
result = data.results[0].geometry.location;
}
});
return result;
}
==================================================================================
================================================== ================================
In light of responses, I have now updated the code to the following. I'm still not getting any result though, with breakpoints set in Firebug the result = data.results[0].geometry.location;never gets hit.
根据响应,我现在已将代码更新为以下内容。我仍然没有得到任何结果,在 Firebug 中设置了断点result = data.results[0].geometry.location; 永远不会被击中。
function loadFromSearch(address)
{
midpoint = getLatLong(address, loadWithMidpoint);
}
function getLatLong(address, callback)
{
var result;
var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
$.getJSON(url,{},
function (data) {
if (data.status == "OK")
{
result = data.results[0].geometry.location;
callback(result);
}
});
}
function loadWithMidpoint(centre)
{
mapCentre = centre;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}
=============================================================================
================================================== ============================
I have it! The final code, which works, looks like this:
我有!最终有效的代码如下所示:
function loadFromSearch(coordinates, address)
{
midpoint = getLatLong(address, latLongCallback);
}
function getLatLong(address, callback)
{
var geocoder = new google.maps.Geocoder();
var result = "";
geocoder.geocode({ 'address': address, 'region': 'uk' }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
result = results[0].geometry.location;
latLongCallback(result);
}
else
{
result = "Unable to find address: " + status;
}
});
return result;
}
function latLongCallback(result)
{
mapCentre = result;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}
回答by Scott
If you are using V3 of the API cannot you use the this?
如果您使用的是 V3 的 API,您不能使用这个吗?
function findAddressViaGoogle() {
var address = $("input[name='property_address']").val();
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
newPointClicked(results[0].geometry.location)
} else {
alert("Unable to find address: " + status);
}
});
}
The above is what I use to find some lat long cordinates of an inputted address, May work better?
以上是我用来查找输入地址的一些经纬度坐标的方法,可能会更好吗?
EDIT:
编辑:
function loadFromSearch(address)
{
midpoint = getLatLong(address);
mapCentre = midpoint;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
}
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 = results[0].geometry.location;
} else {
result = "Unable to find address: " + status;
}
});
return result;
}
回答by DanSingerman
The problem is your $.getJSON
function is asynchronous, yet you are returning the 'result
' synchronously.
问题是您的$.getJSON
函数是异步的,但您正在result
同步返回 ' '。
You need to do something like this (not tested!)
你需要做这样的事情(未经测试!)
function loadFromSearch(address)
{
midpoint = getLatLong(address, function(midpoint){
// this is a callback
mapCentre = midpoint;
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
...
});
}
function getLatLong(address, callback)
{
var result;
var url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&sensor=false'
$.getJSON(url,
function (data) {
if (data.status == "OK") {
result = data.results[0].geometry.location;
callback(result) // need a callback to get the asynchronous request to do something useful
}
});
}
In response to your edit: Oh dear, it looks like the V3 geocoder does not support JSONP. This means you can not do a cross-domain request to get data from it in your browser. See http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/
回应您的编辑:哦,亲爱的,V3 地理编码器似乎不支持 JSONP。这意味着您无法在浏览器中执行跨域请求以从中获取数据。见http://blog.futtta.be/2010/04/09/no-more-jsonp-for-google-geocoding-webservice/
However Brady's solution does work. I guess that is the way Google want us to geocode now.
但是 Brady 的解决方案确实有效。我想这就是 Google 现在希望我们进行地理编码的方式。