我有一个Google地图实施,它使用Google Maps Utility Library v3(http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html)进行群集标记。它运作得很好。
现在,我想在标记中添加infowindows。群集标记可以正常工作,所以我想保持行为(即单击它们时放大)但是当点击普通单个标记时我想显示一个infowindow。
完整的代码如下:
var _iconCenter = new google.maps.MarkerImage('/css/img/map-marker.png',
new google.maps.Size(38, 48),
new google.maps.Point(0,0),
new google.maps.Point(19, 44));
var shadow = new google.maps.MarkerImage('/css/img/map-marker-shadow.png',
new google.maps.Size(57, 49),
new google.maps.Point(0,0),
new google.maps.Point(7, 44));
var _icon = '/css/img/map-marker-purple.png';
var infowindow;
var markersArray = [];
var map;
var currentPosition = 0;
var currentmarker;
var firstload = true;
var maploaded = false;
var interval = 5000;
var geocoder;
var stylez = [];
function initialize(items,loop,zoom) {
geocoder = new google.maps.Geocoder();
if (items.length > 0) {
var latlng = new google.maps.LatLng(items[0].Lat, items[0].Lng);
var myOptions = {
zoom: zoom,
center: latlng,
//mapTypeControl: false,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), myOptions);
map.setOptions({styles: stylez});
for (var i = 0; i < items.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(items[i].Lat, items[i].Lng),
title: items[i].Title,
icon: _icon,
shadow: shadow,
infocontent: items[i].Description
});
marker.setMap(map);
markersArray.push(marker);
}
//set style options for marker clusters (these are the default styles)
mcOptions = {
gridSize: 44
}
var markerCluster = new MarkerClusterer(map, markersArray, mcOptions);
google.maps.event.addListener(map, "tilesloaded", function () {
if(loop == true){
SetLoop();
}
});
google.maps.event.addListener(marker, "click", function () {
alert('test');
//infowindow.setContent('test');
//infowindow.open(map,this);
});
}
}
function SetLoop() {
//This will fire everytime map loads or recenteres
maploaded = true;
if (firstload) {
firstload = false;
Recenter();
}
}
function Recenter() {
//If previous iteration is not loaded completely, wait to avoid errors
//It could happen for slow internet connection
if (maploaded) {
maploaded = false;
} else {
//keep adding 1 second to interval for slow connection till page loads
interval = interval + 1;
setTimeout("Recenter()", interval);
return;
}
if (infowindow != null && currentmarker != null) {
//currentmarker.icon = _icon;
currentmarker.icon = _iconCenter;
currentmarker.setMap(map);
infowindow.close(map, currentmarker);
}
markersArray[currentPosition].icon = _iconCenter;
markersArray[currentPosition].setMap(map);
map.setCenter(new google.maps.LatLng(markersArray[currentPosition].getPosition().lat(), markersArray[currentPosition].getPosition().lng()));
infowindow = new google.maps.InfoWindow({
content: markersArray[currentPosition].infocontent,
size: new google.maps.Size(50, 50)
});
infowindow.open(map, markersArray[currentPosition]);
currentmarker = markersArray[currentPosition];
if (currentPosition >= markersArray.length - 1) {
currentPosition = 0;
} else {
currentPosition++;
}
if (markersArray.length > 1) {
setTimeout("Recenter()", interval);
}
}
正如您所看到的,仅出于演示目的,我在标记点击侦听器上调用粗略警报事件,但它根本不会触发。当我点击普通标记并且浏览器的开发人员工具控制台中没有错误时,没有任何反应。我想知道这是否与另一个事件听地图加载有关,但我处于对此理解的边缘,没有任何错误我很难知道如何调试它。
有人能指出我正确的方向让这个工作吗?
谢谢大家!
答案 0 :(得分:2)
我认为问题是你在添加事件监听器之前将标记交给了集群器。尝试在创建标记后立即添加侦听器,以便在将markersArray传递给clusterer时,事件列表获取器已经附加。 所以试试:
for (var i = 0; i < items.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(items[i].Lat, items[i].Lng),
title: items[i].Title,
icon: _icon,
shadow: shadow,
infocontent: items[i].Description
});
marker.setMap(map);
//attach the listener now, before pushing into the array
attachListener(marker,'marker:'+i);
markersArray.push(marker);
}
然后:
function attachListener(marker,content){
google.maps.event.addListener(marker, "click", function () {
// alert('test');
infowindow.setContent(content);
infowindow.open(map,this);
});
}
答案 1 :(得分:1)
Working example聚类(来自xml文件),标记上有infowindows。