【发布时间】:2015-05-02 00:46:43
【问题描述】:
我写了下面的例子:
http://jsfiddle.net/214190tj/1/
html:
<label for="searchTextField">Please Insert an address:</label>
<br>
<input id="searchTextField" type="text" size="50">
<input type="submit" value="is valid">
js:
var input = document.getElementById('searchTextField');
var options = {componentRestrictions: {country: 'us'}};
new google.maps.places.Autocomplete(input, options);
现在它运行良好,但我需要检查当按钮单击时 uset 没有键入类似“dsgfdsgfjhfg”的内容。
请帮助改进我的代码。
附言
这大约是我想要的,但它在回调中执行。我需要一个返回 true 或 false 的函数。
function codeEditAddress(id) {
var address = document.getElementById('address' + id).value;
isValid = undefined;
geocoder.geocode({ 'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
$("#mapLat" + id).val(results[0].geometry.location.lat());
$("#mapLng" + id).val(results[0].geometry.location.lng());
if (marker) {
marker.setMap(null);
}
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
marker.setMap(map);
isValid = true;
} else {
isValid = false;
}
});
}
【问题讨论】:
-
您希望如何验证输入?我的意思是,好的地址与坏地址的规则是什么?
-
@Pointy 我想验证谷歌是否理解输入地址
-
@Pointy 请阅读更新
-
只要您必须异步请求源,您就无法从函数中获得响应作为返回。
-
在我看来,“Autocomplete()”API 不会告诉您地址是否无效;它所做的只是在用户选择地址时触发一个事件。无论如何,再一次,您不能从这样的异步回调系统返回值。这根本不可能。相反,您必须传入自己的回调来处理“isValid”标志。
标签: javascript google-maps google-maps-api-3 geocoding