谷歌地图(v3)信息窗口始终在同一标记上打开
我创建了几个 Gmarkers(通过 JQuery“load”函数加载的 JSON 数据),在所有这些 Gmarker 上添加一个事件侦听器来打开我之前在标记上创建的 infowindow 对象,然后将它们全部添加到地图中。
问题是信息窗口总是在相同的标记上打开。 我以前都这样工作过,我看不出问题出在哪里......变量的范围?某个地方犯了愚蠢的错误?
我上传了一个示例,这是一个javascript 文件的快捷方式
代码:
var map;
var infowindow;
$(document).ready(function() {
var myLatlng = new google.maps.LatLng(47.15984,2.329102);
var myOptions = {
zoom: 6,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.HYBRID,
scrollwheel: false
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
infowindow = new google.maps.InfoWindow({content:'<p>Test</p>'});
$.getJSON("data.json", function(data) {
var markers = [];
for (var i = data.length - 1; i >= 0; i--){
var latLng = new google.maps.LatLng(data[i].lat, data[i].lng);
var marker = new google.maps.Marker({position: latLng});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
markers.push(marker);
};
for (var j = markers.length - 1; j >= 0; j--){
markers[j].setMap(map);
};
});
});
I create several Gmarkers (from JSON data loaded by JQuery "load" function), on all of them I add an event listener to open the infowindow object I created before on the marker, and then I add them all to the map.
The issue is that the infowindow always opens on the same marker.
I all had this working before, i can't see where the problem is... scope of the variable ? stupid mistake somewhere ?
I uploaded an example, and here is a shortcut to the javascript file
The code :
var map;
var infowindow;
$(document).ready(function() {
var myLatlng = new google.maps.LatLng(47.15984,2.329102);
var myOptions = {
zoom: 6,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.HYBRID,
scrollwheel: false
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
infowindow = new google.maps.InfoWindow({content:'<p>Test</p>'});
$.getJSON("data.json", function(data) {
var markers = [];
for (var i = data.length - 1; i >= 0; i--){
var latLng = new google.maps.LatLng(data[i].lat, data[i].lng);
var marker = new google.maps.Marker({position: latLng});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
markers.push(marker);
};
for (var j = markers.length - 1; j >= 0; j--){
markers[j].setMap(map);
};
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
为
Change
to