Javascript 如何阻止google地图地理代码在N秒后返回
我正在打电话到google.maps.geocoder()获取一些坐标。我想设置一个计时器,在3秒钟后停止geocode调用,但我不知道如何做 这是我的密码Javascript 如何阻止google地图地理代码在N秒后返回,javascript,jquery,google-maps,google-maps-api-3,google-geocoder,Javascript,Jquery,Google Maps,Google Maps Api 3,Google Geocoder,我正在打电话到google.maps.geocoder()获取一些坐标。我想设置一个计时器,在3秒钟后停止geocode调用,但我不知道如何做 这是我的密码 //这里我想停止从.geocode返回的任何呼叫,如果 //计时器在3-5秒后超时。 var navbarGeocoder=新的google.maps.Geocoder(); navbarGeocoder.geocode({//调用geocode “地址”:navbarInput.value },功能(结果、状态){ if(status=
//这里我想停止从.geocode返回的任何呼叫,如果
//计时器在3-5秒后超时。
var navbarGeocoder=新的google.maps.Geocoder();
navbarGeocoder.geocode({//调用geocode
“地址”:navbarInput.value
},功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
$(“#纬度”).val(结果[0].geometry.location.G);
$(“#经度”).val(结果[0].geometry.location.K);
$(“#映射类型”).val(结果[0]。类型[0]);
}
$('form')[0]。提交();
});代码>似乎Geocoder()对象没有公开超时方法。
您可以使用对其URL的jquery ajax调用直接调用其地理编码服务:
从ajax调用服务,这将使您能够更好地控制通信。请参见如果地理编码器在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);
}
});
应该在3秒内有结果。你没有得到一个吗?(它可能处于状态!=OK path)。这怎么不是的副本?对于地理编码,这种方法很好。我想提醒未来的观众,它不适用于标高、方向等API。