Javascript 谷歌地图:多个信息窗口始终显示最后一个值
好吧,我意识到我是第100个问这个问题的人了,但即使在研究和尝试了几天不同的事情之后,我还是想不出来。 我有一个功能,可以在谷歌地图上创建标记。我将向这个函数传递坐标以及显示在infoWindow中的HTML,这些信息应该附加到每个标记上 很多其他人的问题是,即使在我的超级简单的示例中,infoWindow的内容始终是任何infoWindow的最后一个内容集,而不是创建特定标记时的内容集 我怎样才能解决这个问题 这是我的密码:Javascript 谷歌地图:多个信息窗口始终显示最后一个值,javascript,google-maps,google-maps-api-2,Javascript,Google Maps,Google Maps Api 2,好吧,我意识到我是第100个问这个问题的人了,但即使在研究和尝试了几天不同的事情之后,我还是想不出来。 我有一个功能,可以在谷歌地图上创建标记。我将向这个函数传递坐标以及显示在infoWindow中的HTML,这些信息应该附加到每个标记上 很多其他人的问题是,即使在我的超级简单的示例中,infoWindow的内容始终是任何infoWindow的最后一个内容集,而不是创建特定标记时的内容集 我怎样才能解决这个问题 这是我的密码: var somerandomcounter = 0; functi
var somerandomcounter = 0;
function addMarkerNew(){
markers[somerandomcounter] = new GMarker(new GLatLng(52.3666667+somerandomcounter,9.7166667+somerandomcounter),{title: somerandomcounter});
map.addOverlay(markers[somerandomcounter]);
var marker = markers[somerandomcounter];
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml("<b>"+somerandomcounter+"</b>");
});
somerandomcounter++;
}
var-somerandomcounter=0;
函数addMarkerNew(){
markers[somerandomcounter]=新的GMarker(新的GLatLng(52.3666667+somerandomcounter,9.7166667+somerandomcounter),{title:somerandomcounter});
addOverlay(标记[somerandomcounter]);
var marker=标记[somerandomcounter];
addListener(标记“单击”,函数(){
marker.openInfoWindowHtml(“+somerandomcounter+”);
});
somerandomcounter++;
}
这里的问题是可变范围。让我们把它分解一下:
// variable is in the global scope
var somerandomcounter = 0;
function addMarkerNew(){
// now we're in the addMarkerNew() scope.
// somerandomcounter still refers to the global scope variable
// ... (some code elided)
var marker = markers[somerandomcounter];
GEvent.addListener(marker, "click", function() {
// now we're in the click handler scope.
// somerandomcounter *still* refers to the global scope variable.
// When you increment the variable in the global scope,
// the change will still be reflected here
marker.openInfoWindowHtml("<b>"+somerandomcounter+"</b>");
});
// increment the global scope variable
somerandomcounter++;
}
// variable is in the global scope
var somerandomcounter = 0;
// make a new function to attach the handler
function attachClickHandler(marker, counter) {
// now we're in the attachClickHandler() scope.
// counter is a locally scope variable
GEvent.addListener(marker, "click", function() {
// now we're in the click handler scope.
// counter refers to the local variable in
// the attachClickHandler() scope
marker.openInfoWindowHtml("<b>"+counter+"</b>");
});
}
function addMarkerNew(){
// now we're in the addMarkerNew() scope.
// somerandomcounter still refers to the global scope variable
// ...
var marker = markers[somerandomcounter];
// attach the click handler
attachClickHandler(marker, somerandomcounter)
// increment the global scope variable
somerandomcounter++;
}
//变量在全局范围内
var-somerandomcounter=0;
//创建一个新函数来附加处理程序
函数attachClickHandler(标记器、计数器){
//现在我们在attachClickHandler()范围内。
//计数器是一个局部作用域变量
addListener(标记“单击”,函数(){
//现在我们在单击处理程序范围内。
//计数器是指中的局部变量
//attachClickHandler()作用域
marker.openInfoWindowHtml(“+counter+”);
});
}
函数addMarkerNew(){
//现在我们在addMarkerNew()范围内。
//somerandomcounter仍然引用全局范围变量
// ...
var marker=标记[somerandomcounter];
//附加单击处理程序
attachClickHandler(标记器、somerandomcounter)
//递增全局范围变量
somerandomcounter++;
}
顺便说一句,有一种将点击标记绑定到窗口的简写方法@操作,然后停止使用v2。
// variable is in the global scope
var somerandomcounter = 0;
// make a new function to attach the handler
function attachClickHandler(marker, counter) {
// now we're in the attachClickHandler() scope.
// counter is a locally scope variable
GEvent.addListener(marker, "click", function() {
// now we're in the click handler scope.
// counter refers to the local variable in
// the attachClickHandler() scope
marker.openInfoWindowHtml("<b>"+counter+"</b>");
});
}
function addMarkerNew(){
// now we're in the addMarkerNew() scope.
// somerandomcounter still refers to the global scope variable
// ...
var marker = markers[somerandomcounter];
// attach the click handler
attachClickHandler(marker, somerandomcounter)
// increment the global scope variable
somerandomcounter++;
}