【发布时间】:2014-01-06 21:35:17
【问题描述】:
Geocoder 方法在通过按钮单击调用时不返回值。我需要将结果存储在全局变量“source_lat”和“source_long”中。似乎我的按钮单击在地理编码器可以返回值之前结束。
function GeocoderStart(str) {
geocoder = new google.maps.Geocoder();
geocoder_request = { 'address': str };
geocoder.geocode(geocoder_request, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var country = extractFromAdress(results[0].address_components, "country");
if ((country != "India") & (country != "")) {
//alert("Error", "The address you entered refers to <b>" + results[0].formatted_address + "</b>. Please select only addresses in India using the autocomplete lookup.");
alert("Please select a valid address");
return false;
} else {
source_lat = results[0].geometry.location.lat();
source_long = results[0].geometry.location.lng();
}
} else {
alert("Error", "Unable to lookup address for the following reason: " + status);
return false;
}
});
}
//从地址函数中提取
function extractFromAdress(components, type) { // taken from http://stackoverflow.com/questions/8313876/more-efficient-way-to-extract-address-components
for (var i = 0; i < components.length; i++)
for (var j = 0; j < components[i].types.length; j++)
if (components[i].types[j] == type) return components[i].long_name;
return "";
}
【问题讨论】:
-
你得到了什么? 请选择一个有效地址或另一个?的警报
-
@putvande 没有警报。我没有得到任何值。
-
但是你不能从异步方法中返回值。一旦获得价值,就必须在请求回调方法中编写逻辑。这里使用任何全局变量似乎都是错误的
-
有任何错误吗?你怎么知道你没有得到你想要的值?
-
@putvande 我正在尝试使用这些值在我的按钮单击时将其显示在标签中。所以我在那里没有得到任何价值。
标签: jquery api google-maps