gpt4 book ai didi

javascript - 如何通过 javascript 使谷歌地图地理编码器超时?

转载 作者:行者123 更新时间:2023-11-30 18:33:16 25 4
gpt4 key购买 nike

我是 javascript 的新手,我在地理编码器请求超时方面遇到了问题。但我被卡住了,我试图在循环中添加延迟,但似乎它们不起作用。如果您能提供帮助,我将不胜感激。

        <script type="text/javascript">

function setLocationOnMap(locs) {
var myOptions = {
zoom: 4,
center: new google.maps.LatLng(locs.lat(), locs.lng()),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

var i=0;
var total='<?php echo $counter ?>';

for (i=0; i<total;i++){//adding cities to map by
var city= document.the_form.elements[i].value;

getLatLong2(city, map, setPointer);
}
}

function setPointer(map, address, locs2){
var position = new google.maps.LatLng(locs2.lat(), locs2.lng());
var marker = new google.maps.Marker({
position: position,
map: map
});
marker.setTitle(address);
}


function initialize() {
var address = "Chicago IL";
getLatLong(address, setLocationOnMap);
}

function getLatLong2(address, map, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
locs2 = results[0].geometry.location;

callback(map, address, locs2);
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});
}



function getLatLong(address, callback){
var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
// processing...
locs = results[0].geometry.location;
//pausecomp(10000);

callback(locs);
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>

最佳答案

当您提交地理编码请求时,您可以并行启动一个计时器,当计时器触发时,您可以声明请求已超时。请求仍会继续,但您可以在超时后忽略结果:

function getLatLong(address, callback){
var timerId;
var timedOut = false;

var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
if (timedOut) {
// this request timed out, so ignore results
return;
} else {
// this request succeeded, so cancel timer
clearTimeout(timerId);
}

if (status == google.maps.GeocoderStatus.OK) {
locs = results[0].geometry.location;
callback(locs);
} else {
//alert("Geocode was not successful for the following reason: " + status);
}
});

timerId = setTimeout(function() {
timedOut = true;
alert('Request timed out.');
// do something else
}, 10000);
}

关于javascript - 如何通过 javascript 使谷歌地图地理编码器超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com