Javascript Google Maps API v3中标记的循环问题
我不知道这为什么不起作用。我没有任何错误,但是不管我点击什么标记,它总是点击最后一个标记。我不知道为什么,因为_标记是以同样的方式设置的。我怎样才能解决这个问题 (使用新的jQuery+XML更新) 您在以下循环中有:Javascript Google Maps API v3中标记的循环问题,javascript,jquery,xml,google-maps,loops,Javascript,Jquery,Xml,Google Maps,Loops,我不知道这为什么不起作用。我没有任何错误,但是不管我点击什么标记,它总是点击最后一个标记。我不知道为什么,因为_标记是以同样的方式设置的。我怎样才能解决这个问题 (使用新的jQuery+XML更新) 您在以下循环中有: for(x in locations){ console.log(x); infowindow[x] = new google.maps.InfoWindow({content: x}); marker[x] = new google.maps.Marker({
for(x in locations){
console.log(x);
infowindow[x] = new google.maps.InfoWindow({content: x});
marker[x] = new google.maps.Marker({title:locations[x][0],map:map,position:locations[x][2]});
google.maps.event.addListener(marker[x], 'click', function() {infowindow[x].open(map,marker[x]);});
}
闭包中包含的变量共享同一个环境,因此在执行click
回调时,循环已经运行,并且x
变量将指向最后一个条目
您可以使用函数工厂使用更多闭包来解决此问题:
function makeInfoWindowEvent(map, infowindow, marker) {
return function() {
infowindow.open(map, marker);
};
}
for(x in locations){
infowindow[x] = new google.maps.InfoWindow({content: x});
marker[x] = new google.maps.Marker({title: locations[x][0],
map: map,
position: locations[x][3]});
google.maps.event.addListener(marker[x], 'click',
makeInfoWindowEvent(map, infowindow[x], marker[x]);
}
如果您不熟悉闭包的工作方式,这可能是一个相当棘手的话题。您可以查看以下Mozilla文章以获得简要介绍:
更新:
更进一步的问题,你应该考虑如下:
- 首先,请记住JavaScript没有块作用域。只有函数才有作用域
- 当您分配一个以前未使用
关键字声明的变量时,它将被声明为全局变量。这通常被认为是JavaScript的一个丑陋的特性(或缺陷),因为它可以隐藏许多bug。因此,这应该避免。这些隐含的全局变量有两个实例:var
和the_marker
,事实上,这就是程序失败的原因infowindow
- JavaScript有闭包。这意味着内部函数可以访问外部函数的变量和参数。这就是为什么您可以从
方法的回调函数访问addListener
、标记
和infowindow
。但是,由于您的map
和标记
被视为全局变量,因此闭包不起作用信息窗口
var
关键字,如下例所示:
$(function() {
var latlng = new google.maps.LatLng(45.522015,-122.683811);
var settings = {
zoom: 15,
center: latlng,
disableDefaultUI: true,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
$.get('mapdata.xml', {}, function(xml) {
$('location', xml).each(function(i) {
var the_marker = new google.maps.Marker({
title: $(this).find('name').text(),
map: map,
clickable: true,
position: new google.maps.LatLng(
parseFloat($(this).find('lat').text()),
parseFloat($(this).find('lng').text())
)
});
var infowindow = new google.maps.InfoWindow({
content: $(this).find('description').text();
});
new google.maps.event.addListener(the_marker, 'click', function() {
infowindow.open(map, the_marker);
});
});
});
});
这是我的方法
for(x in locations){
var name = locations[x][0];
var latlng = locations[x][3];
addMarker(map, name, latlng);
}
function addMarker(map, name, latlng){
var infoWin = new google.maps.InfoWindow({content: name});
var marker = new google.maps.Marker({
map: map,
position: latlng,
title: name
});
google.maps.event.addListener(marker, 'click', function(){
infoWin.open(map, marker);
});
}
华盛顿西南1201-1299是什么?波特兰还是97205?@BalusC:创新住宅::)哈哈,说实话,我刚刚加入了come坐标,以便能很好地看到波特兰市:)对不起,你能给我看看我现在的代码吗?我有点困惑谢谢你的帮助。我必须更新它才能使用jQuery和XML文件。@Oscar:更新了我的答案。您只需使用
var
关键字声明您的标记
和infowindow
变量。看起来此代码不会在打开第二个infowindow时关闭上一个infowindow,是真的吗@DanielVassallo
for(x in locations){
var name = locations[x][0];
var latlng = locations[x][3];
addMarker(map, name, latlng);
}
function addMarker(map, name, latlng){
var infoWin = new google.maps.InfoWindow({content: name});
var marker = new google.maps.Marker({
map: map,
position: latlng,
title: name
});
google.maps.event.addListener(marker, 'click', function(){
infoWin.open(map, marker);
});
}