我正在调用google.maps.geocoder()来获取一些坐标。我想设置一个计时器,在3秒后停止地理编码调用,但我不确定该怎么做?
这是我的代码。
// here I want to stop any calls coming back from .geocode if a
//timer times out after say 3-5 seconds.
var navbarGeocoder = new google.maps.Geocoder();
navbarGeocoder.geocode({ // call to geocode
'address': navbarInput.value
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$("#latitude").val(results[0].geometry.location.G);
$("#longitude").val(results[0].geometry.location.K);
$("#mapType").val(results[0].types[0]);
}
$('form')[0].submit();
});
发布于 2015-08-23 04:07:16
它看起来不像是Geocoder()对象公开了一个超时方法。您可以使用jquery ajax调用其URL直接调用其地理编码服务:https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY
从ajax调用服务,这将使您能够更好地控制通信。请参阅http://api.jquery.com/jquery.ajax/
发布于 2015-08-23 05:24:23
如果地理编码器在3秒内没有返回结果(通过或失败),那么一定是出了问题。只有在地理编码成功时才提交表单,否则不要提交表单,否则可能会让用户知道出了问题。
// here I want to stop any calls coming back from .geocode if a
//timer times out after say 3-5 seconds.
var navbarGeocoder = new google.maps.Geocoder();
navbarGeocoder.geocode({ // call to geocode
'address': navbarInput.value
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$("#latitude").val(results[0].geometry.location.G);
$("#longitude").val(results[0].geometry.location.K);
$("#mapType").val(results[0].types[0]);
// only submit the form it the geocoder succeeded
$('form')[0].submit();
} else {
// let the user know the request failed
alert("geocoder failed, status = "+status);
}
});https://stackoverflow.com/questions/32160097
复制相似问题