我在一个阵列中有19个邮政编码通过GeoCode,它似乎工作正常但只有11个显示。我搜索并尝试了一切。我刚刚开始学习JavaScript和地图API,所以我确信我的拼写错误。
var latlng = new google.maps.LatLng(55.378, -3.435);
myOptions = {
zoom: 5,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("my_canvas"),myOptions);
var geocoder = new google.maps.Geocoder();
var places = ["AB12 3AG","BS11 8AT","IG11 0HN","CA3 0PJ","NR19 1JG","IP11 3HZ","ML4 3LR","WF12 7TH","L33 7YE","M30 9QG","NG18 5DQ","MK12 5QL","PE6 0BN","ME10 3RL","SO40 9HN","B79 7TD","TS2 1RP","GU17 0NP","WV10 7EL"];
for(i = 0; i < places.length; i++){
var address = places[i];
geocoder.geocode({'address':address}, function(results){
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: address
});
});
}
我认为所有邮政编码都会加载,因为所有标记的标题都是数组'WV10'中的最后一个地址,但就像我说的那样,只显示了19个地点中的11个。
答案 0 :(得分:0)
问题是你在循环中创建一个函数。至少这是标签的问题。解决方案是创建一个新的范围来调用循环中的函数:
for(i = 0; i < places.length; i++){
(function(place) {
geocoder.geocode({'address':place}, function(results){
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title: place
});
});
}(places[i]));
}
这也可以解决显示问题,但我不确定。难道有些地方会解决相同或非常接近的位置吗?