谷歌地图 infowindows 始终附加到第一个标记
我正在循环数组以创建标记和信息窗口,但信息窗口始终附加到第一个标记...(即使您单击另一个标记)
function initialize() {
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(50.902946,4.241436),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
setMarkers(map, shops);
}
var shops = [['United Brands', 51.218342, 4.405464, 4, 'ZZEERRR'],['United Brands', 51.162582, 4.991303, 4, 'ZZEERRR'],['United Brands Hasselt', 50.929314, 5.339501, 4, 'ZZEERRR'],['United Brands Oud-Turnhout', 51.320259, 4.974206, 4, 'ZZEERRR'],['United Brands Leuven', 50.879509, 4.705221, 4, 'ZZEERRR'],['United Brands Veurne', 51.083534, 2.644152, 4, 'ZZEERRR'],['United Brands Wijnegem', 51.222084, 4.498419, 4, 'ZZEERRR'],['United Brands Knokke', 51.340641, 3.234586, 4, 'ZZEERRR'],['United Brands Aalst', 50.938343, 4.034963, 4, 'ZZEERRR'],['UBextreme', 51.052570, 3.723077, 4, 'ZZEERRR'],['UBextreme', 51.218273, 4.402394, 4, 'ZZEERRR']];
function setMarkers(map, locations) {
for (var i = 0; i < locations.length; i++) {
var shop = locations[i];
var myLatLng = new google.maps.LatLng(shop[1], shop[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
animation: google.maps.Animation.DROP,
title: shop[0],
zIndex: shop[3],
clickable: true
});
var infowindow = new google.maps.InfoWindow({ content: shop[4] });
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
}
initialize();
i'm looping over an array to create markers and infowindows but the infowindow is always attached to the first marker... (even if you click another marker)
function initialize() {
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(50.902946,4.241436),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
setMarkers(map, shops);
}
var shops = [['United Brands', 51.218342, 4.405464, 4, 'ZZEERRR'],['United Brands', 51.162582, 4.991303, 4, 'ZZEERRR'],['United Brands Hasselt', 50.929314, 5.339501, 4, 'ZZEERRR'],['United Brands Oud-Turnhout', 51.320259, 4.974206, 4, 'ZZEERRR'],['United Brands Leuven', 50.879509, 4.705221, 4, 'ZZEERRR'],['United Brands Veurne', 51.083534, 2.644152, 4, 'ZZEERRR'],['United Brands Wijnegem', 51.222084, 4.498419, 4, 'ZZEERRR'],['United Brands Knokke', 51.340641, 3.234586, 4, 'ZZEERRR'],['United Brands Aalst', 50.938343, 4.034963, 4, 'ZZEERRR'],['UBextreme', 51.052570, 3.723077, 4, 'ZZEERRR'],['UBextreme', 51.218273, 4.402394, 4, 'ZZEERRR']];
function setMarkers(map, locations) {
for (var i = 0; i < locations.length; i++) {
var shop = locations[i];
var myLatLng = new google.maps.LatLng(shop[1], shop[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
animation: google.maps.Animation.DROP,
title: shop[0],
zIndex: shop[3],
clickable: true
});
var infowindow = new google.maps.InfoWindow({ content: shop[4] });
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
}
initialize();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
变量“map”现在超出了 setMarker 的范围
try this:
The variable "map" now is out of setMarker's scope