如果我将它们编码为一次添加一个,我已经成功地向我添加到谷歌地图(V3)的折线的点击事件添加了一个处理程序,但是如果我创建一个 for 循环来简单地遍历我的数据,它失败。
工作演示:
http://www.ikfoundation.org/demo/works.html
http://www.ikfoundation.org/demo/fails.html
除了画线的位之外,每个代码都是相同的:
作品:
var linePts = [[
new google.maps.LatLng(59.454924068851290, 17.094726562500000),
new google.maps.LatLng(55.984639327677950, 17.270507812500000),
],[
new google.maps.LatLng(51.081191044453350, 26.938476562500000),
new google.maps.LatLng(62.112946929861720, 26.586914062500000)
]];
// Draw the lines...
elines[0] = new google.maps.Polyline({
path: linePts[0],
strokeColor: "#0000ff",
strokeOpacity: 1.00,
strokeWeight: 7,
clickable: true,
editable: true,
geodesic: true,
zIndex: 1,
map: map,
myID: 0
});
google.maps.event.addListener(elines[0], 'click', function()
{
lineClick(elines[0]);
});
elines[1] = new google.maps.Polyline({
path: linePts[1],
strokeColor: "#0000ff",
strokeOpacity: 1.00,
strokeWeight: 7,
clickable: true,
editable: true,
geodesic: true,
zIndex: 1,
map: map,
myID: 1
});
google.maps.event.addListener(elines[1], 'click', function()
{
lineClick(elines[1]);
});
function lineClick(line)
{
alert("Line clicked with myID=" + line.myID);
}
失败(我排除了与上面显示的相同的线点定义数组,以及 lineClick 函数,两者再次相同):
for (var i=0; i<=1; i++)
{
elines[i] = new google.maps.Polyline({
path: linePts[i],
strokeColor: "#0000ff",
strokeOpacity: 1.00,
strokeWeight: 7,
clickable: true,
editable: true,
geodesic: true,
zIndex: 1,
map: map,
myID: i
});
google.maps.event.addListener(elines[i], 'click', function()
{
lineClick(elines[i]);
});
}
我做错了什么?两者都在这些变量中使用相同的变量名称和索引。正如您在我的示例链接中看到的那样,除了点击侦听器之外,一切都运行良好。在后一个“失败”版本中失败,因为似乎折线根本没有传递给 lineClick 函数(您需要运行 java 调试器才能看到错误)。
谢谢!