谷歌地图:多个InfoWindows总是显示最后的值
Google Maps: Multiple InfoWindows always show last value
好吧,我意识到我是第100个问这个问题的人,但即使经过几天的研究和尝试,我还是不明白。我有一个功能,将创建标记在谷歌地图上。我将向该函数传递坐标以及将显示在infoWindow中的HTML,它们应该附加到每个标记上。
很多人遇到的问题是,即使在我的超级简单的例子中,infoWindow的内容总是任何infoWindow的最后一个内容集,而不是创建特定标记时的内容集。
我该如何解决这个问题?
下面是我的代码:
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++;
}
这里的问题是变量范围。让我们来分析一下:
// 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++;
}
解决这个问题的最简单方法是将somerandomcounter
变量作为参数传递给其中一个函数——这将使click处理程序中的引用指向局部作用域变量。这里有两种方法:
将计数器作为参数传递给
addMarkerNew
:// variable is in the global scope var somerandomcounter = 0; function addMarkerNew(counter){ // now we're in the addMarkerNew() scope. // counter is in the local scope // ... var marker = markers[counter]; GEvent.addListener(marker, "click", function() { // now we're in the click handler scope. // counter *still* refers to the local addMarkerNew() variable marker.openInfoWindowHtml("<b>"+somerandomcounter+"</b>"); }); } // call the function, incrementing the global variable as you do so addMarkerNew(somerandomcounter++);
创建一个新函数来附加click处理程序,并将计数器传递给该函数:
// 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++; }
相关文章:
- dataTables-如何自定义分页类型以显示最后一个页码后面的省略号,
- 谷歌地图-数据库中的标记只显示最后一行的信息
- jQuery自定义验证只显示最后一个字段的错误
- 有角度的ng重复只显示最后一个元素.如果只有一个元素;我什么也不展示
- AngularJS:仅在最后一个选项卡上显示过滤的内容
- 使用 Apache 模块和 Javascript/jQuery 显示网页的最后修改日期
- 谷歌可视化散点图只显示最后一个系列的工具提示
- "重新打开最后一个关闭的选项卡“;导致显示最后一个ajax请求内容
- 单击DIV上的所有ID时如何显示最后一个ID
- 如何使用Selenium IDE存储和键入显示文本的最后一个单词
- 使用java脚本显示最后一次看到的网页状态
- 在JavaScript中,console.log给出了for循环的每个值.但是var.text只显示最后一个值.这是为什
- 谷歌地图:多个InfoWindows总是显示最后的值
- 通过Array的For循环只显示最后一个值
- D3 -在轴上只显示中间和最后的标记
- 要显示数据的最后一周和任何两个日期之间的数据,我正在努力
- 如何在最后一个选项卡中显示页面的填充表单值
- 我在一个对象中创建了两个不同的对象.在打印它们时,只显示最后修改的对象.如何创造
- 嵌套的for循环总是显示最后的数据
- For循环通过数组只显示最后的值,谷歌地图