。 我有来自sql的50个地址。每个地址都将在div LocationAddress中呈现。我遍历每一个,地理编码并在地图中绘制标记。但它只显示了11个。我知道这是因为地理编码器存在瓶颈。如何使用setinterval或settimeout来控制它?我研究并看到人们使用settimeout来处理这个问题。请有人指导我......
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(42.095287, -79.3185139);
var myOptions = {
maxZoom: 14,
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
function codeAddress() {
var infowindow = new google.maps.InfoWindow({});
$('.LocationAddress').each(function() {
var addy = $(this).text();
geocoder.geocode( { 'address': addy}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:addy,
});
//Adding a click event to the marker
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div id=\"infowindow\" style=" height:100px;>'
+'<div id=\"LeftInfo\">'+ "Hello World!"
+'</div>'+'</div>');
infowindow.open(map, this);
});
}
});//Geocoder END
});
}
答案 0 :(得分:1)
我实际编码并进行了测试,这个有效:
var geocoder;
var map;
var addresses = new Array();
var infowindow;
var theInterval;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(42.095287, -79.3185139);
var myOptions = {
maxZoom: 14,
zoom: 9,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
infowindow = new google.maps.InfoWindow({});
}
$(document).ready(function () {
getAddresses();
theInterval = setInterval("codeAddress()", 1000);
});
function getAddresses () {
$('.LocationAddress').each(function () {
addresses.push($(this).text());
});
}
function codeAddress() {
if (addresses.length == 0) {
clearInterval(theInterval);
}
var addy = addresses.pop();
geocoder.geocode({
'address': addy
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: addy,
});
//Adding a click event to the marker
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent('<div id=\"infowindow\" style=" height:100px;>' + '<div id=\"LeftInfo\">' + "Hello World!" + '</div>' + '</div>');
infowindow.open(map, this);
});
}
}); //Geocoder END
}
答案 1 :(得分:0)
您可以序列化请求,以便一次处理一个地址。完成后,您将处理下一个。这可以这样做:
function codeAddress() {
var infowindow = new google.maps.InfoWindow({});
var addresses = $('.LocationAddress');
var addressIndex = 0;
function next() {
if (addressIndex < addresses.length) {
var addy = addresses.eq(addressIndex).text();
++addressIndex;
geocoder.geocode( { 'address': addy}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:addy,
});
//Adding a click event to the marker
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div id=\"infowindow\" style=" height:100px;>'
+'<div id=\"LeftInfo\">'+ "Hello World!"
+'</div>'+'</div>');
infowindow.open(map, this);
});
next();
}
});//Geocoder END
}
}
next();
}