流星 - 谷歌地图信息窗口事件未触发
Meteor - Google Maps InfoWindow Event Not Firing
使用 Meteorjs 和 dburles Google Map Package for Meteor。
目标:是让标记在画布上映射,然后单击以显示信息窗口。我在触发谷歌地图事件以使窗口显示时遇到问题。
我的代码:
Template.myMap.helpers({
mapOptions: function() {
var locations = [
['Kroger', 34.069201, -84.231052, 5],
['Fresh Produce', 34.069802, -84.234164, 4],
['Starbucks', 34.069003, -84.236323, 3],
['Mall of Georgia', 34.069204, -84.232016, 2],
['Avalanche', 34.069705, -84.238207, 1]
]
// Make sure the maps API has loaded
if (GoogleMaps.loaded()) {
// We can use the `ready` callback to interact with the map API once the map is ready.
GoogleMaps.ready('myMap', function(map) {
var infowindow = new google.maps.InfoWindow(),
marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map.instance
});
google.maps.event.addListener(marker, 'click', function() {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
});
}
});
return {
center: new google.maps.LatLng(34.069705, -84.238),
zoom: 16
};
}
}
});
您从事件处理程序返回一个函数,而不仅仅是处理事件,原因尚不清楚。 试试这个:
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
});
更新
另一个问题是你试图把它添加到谷歌地图对象,而不是实例本身。 它应该是:
infowindow.open(map.instance, marker);
另外,我还没有运行你的代码,但我认为你会遇到构建它的方式的问题,因为当处理程序实际被触发时,i
的值将不符合预期(因为循环随后会继续)。 请考虑改用forEach
(下面通过下划线),以便每个处理程序都在其自己的闭包中声明。
_.forEach(locations, function(location) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(location[1], location[2]),
map: map.instance
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(location[0]);
console.log(map, marker, infowindow);
infowindow.open(map.instance, marker);
});
});
相关文章:
- 召回窗口加载事件 - javascript
- node-webkit-从父窗口捕获iframe鼠标事件
- 正在尝试处理子窗口上的关闭窗口事件
- 窗口大小调整事件在ie7中持续触发
- 为什么不'我的窗口滚动事件根本没有启动.其他答案没有解决问题
- 如何在Ol3弹出窗口中添加用javascript创建的按钮上的点击事件
- 点击AngularJS模态窗口捕捉背景事件
- 检测window.opener窗口刷新事件
- 我可以在FullCalendar中设置事件ClickLimit弹出窗口的样式吗
- 如何在事件函数中访问窗口实例
- 信息窗口谷歌地图点击事件给出错误
- 从父级触发弹出窗口中的事件
- 如何在窗口更改时触发事件
- 剑道窗口还原在最小化然后还原时不调用调整大小事件
- 销毁窗口.重新调整事件订阅服务器的大小
- AngularJS窗口滚动事件未触发
- 控制窗口.obeforeunload事件
- 如何跨窗口传递事件,可能
- 在聊天窗口中检测链接单击事件
- 保留动态创建的下拉列表's在事件窗口.history.back()上选择的值-JavaScript