标签:javascript google-maps-api-3 geocoding
这里是链接:http://alchemist3d.com/maptest.html
我也在循环中使用地址解析器来获取地址数组的坐标,这是代码:
function initialize() {
var list = [
{location:"residencial punta del sol casa 6 temixco Morelos Mexico",body : " 1", title : "m 1"},
{location:"prol. harris num. 23 ampl. bugambilias jiutepec Morelos Mexico",body : "ampl. bugambilias 2", title : "f 2"},
{location:"Gladiola Satelite Cuernavaca Morelos Mexico",body:"Montes de Oca"}
];
var latlng = new google.maps.LatLng(18.92009,-99.20611);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//map.fitBounds(getBounds());
codeLocations(list, map);
}
function codeLocations(list, map) {
for (var i = 0; i < list.length; i++) {
//console.log("Looping " + list[i].location);
var geocoder = new google.maps.Geocoder();
var geoOptions = {
address: list[i].location,
bounds: getBounds(),
region: "NO"
};
geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map));
}
}
function createGeocodeCallback(item, map) {
//console.log("Generating geocode callback for " + item.location);
return function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//console.log("Geocoding " + item.location + " OK");
addMarker(map, item, results[0].geometry.location);
} else {
//console.log("Geocode failed " + status);
}
}
}
function addMarker(map, item, location) {
//console.log("Setting marker for " + item.location + " (location: " + location + ")");
var marker = new google.maps.Marker({ map : map, position : location});
marker.setTitle(item.title);
var infowindow = new google.maps.InfoWindow( {
content : item.body,
size : new google.maps.Size(100, 300)
});
new google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
}
function getBounds() {
var myOptions = {
zoom: 23,
center: new google.maps.LatLng(-33.9, 151.2),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var southwest = new google.maps.LatLng(17.920,-100.206);
var northeast =new google.maps.LatLng(19.920,-104.206);
return new google.maps.LatLngBounds(southwest, northeast);
}
解决方法:
是的,有一个速率限制,但记录不充分.每秒9-10个请求?如果您检查错误状态,则称为OVER_QUERY_LIMIT.有100个标记,我认为通用的等待技术不会对您有多大好处,因为您有很多标记.最好进行一次地理编码,保存并使用LatLng.
如果您很勇敢,可以尝试:
setTimeout(function(){
geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map));
}, 200*i);
我不知道为什么每个人都使用200毫秒,也许是反复试验?您可以尝试150或100.不过,在100毫秒时,要花费10秒才能加载100个标记.我读到的另一个想法是分批查询.地址解析为十,请稍等,然后再十.
您可以尝试的另一种功能是睡眠.
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
标签:javascript,google-maps-api-3,geocoding 来源: https://codeday.me/bug/20191012/1900082.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。