0

我有一个 Google Maps 实现,它使用 Google Maps Utility Library v3 (http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/reference.html) 对标记进行聚类。它完美地工作。

不过,现在我想将 infowindows 添加到标记中。集群标记按原样工作正常,所以我想保持这些行为(即,当您单击它们时它们会放大)但是当单击普通的单个标记时,我想显示一个信息窗口。

完整代码如下:

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);
  }
}

如您所见,仅出于演示目的,我在标记单击侦听器上调用了一个粗略的警报事件,但它根本没有触发。当我单击正常标记并且浏览器的开发人员工具控制台中没有错误时,什么也没有发生。我想知道这是否与让另一个事件监听地图加载有关,但我对这一点的理解处于边缘,并且没有任何错误,我正在努力知道如何调试它。

谁能指出我正确的方向来完成这项工作?

谢谢各位!

4

2 回答 2

2

我认为问题在于您在添加事件侦听器之前将标记交给集群器。尝试在创建标记后立即添加侦听器,这样当您将标记数组传递给集群器时,事件侦听器已经附加。所以试试:

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);
        });

    }
于 2012-08-15T17:24:31.717 回答
1

集群的工作示例(来自 xml 文件),标记上有信息窗口。

于 2012-08-15T18:04:15.637 回答