使用MarkerClusterer的Marker上的InfoWindow

时间:2011-09-28 09:15:12

标签: html google-maps-markers infowindow

这是我的HTML代码。我试着在标记上添加一个信息窗口,但它不想工作。我的数据是从“Alle_Ortswahlen.page1.xml”文件加载的。 有谁知道如何将infoWindow添加到每个标记?

<script type="text/javascript">
  google.load('maps', '3', {
    other_params: 'sensor=false'
  });
  google.setOnLoadCallback(initialize);
function initialize() {

  var stack = [];

  var center = new google.maps.LatLng(48.136, 11.586);
    var options = {
        'zoom': 5,
        'center': center,
        'mapTypeId': google.maps.MapTypeId.ROADMAP
    };
  var map = new google.maps.Map(document.getElementById("map_canvas"), options);
  GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
    var xmlDoc = GXml.parse(doc);
    var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
    for (var i = 0; i < markers.length; i++) {
      // obtain the attribues of each marker
      var lat = parseFloat(markers[i].getAttribute("Field4"));
      var lng = parseFloat(markers[i].getAttribute("Field6"));
      var marker = new google.maps.Marker({
            position : new google.maps.LatLng(lat, lng),
            map: map,
            title:"This is a marker"
        });     
      stack.push(marker);
    }
    var mc = new MarkerClusterer(map,stack);
  });
}
</script>

2 个答案:

答案 0 :(得分:7)

在for循环之前,创建一个空的infowindow对象。

var infowindow = new google.maps.InfoWindow();

在for循环中,在标记之后添加一个事件监听器,如下所示:

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent("You might put some content here from your XML");
                    infowindow.open(map, marker);
                }
            })(marker, i));

将回调参数传递给addListener方法时会发生一些闭包魔术。如果你不熟悉它,请看这里:

Mozilla Dev Center: Working with Closures

所以,你的代码应该是这样的:

google.load('maps', '3', {
            other_params: 'sensor=false'
        });

        google.setOnLoadCallback(initialize);

        function initialize() {

            var stack = [];

            var center = new google.maps.LatLng(48.136, 11.586);
            var options = {
                'zoom': 5,
                'center': center,
                'mapTypeId': google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("map_canvas"), options);
            var infowindow = new google.maps.InfoWindow();
            GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
                var xmlDoc = GXml.parse(doc);
                var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
                for (var i = 0; i < markers.length; i++) {
                    // obtain the attribues of each marker
                    var lat = parseFloat(markers[i].getAttribute("Field4"));
                    var lng = parseFloat(markers[i].getAttribute("Field6"));
                    var marker = new google.maps.Marker({
                        position : new google.maps.LatLng(lat, lng),
                        map: map,
                        title:"This is a marker"
                    });     
                    google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            infowindow.setContent("You might put some content here from your XML");
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                    stack.push(marker);
                }
                var mc = new MarkerClusterer(map,stack);
            });
        }

答案 1 :(得分:1)

所以你需要做的是在你的for循环中添加一些代码,将infowindow onclick事件处理程序与每个标记相关联。我假设您只希望一次显示1个信息窗口,即您点击标记,信息窗口会显示相关内容。如果您随后单击另一个标记,则第一个信息窗口将消失,并且新的信息将再次出现在另一个标记上。而不是让多个信息窗口同时可见。

GDownloadUrl("Alle_Ortswahlen.page1.xml", function(doc) {
    var xmlDoc = GXml.parse(doc);
    var markers = xmlDoc.documentElement.getElementsByTagName("ROW");
    // just create one infowindow without any content in it
    var infowindow =  new google.maps.InfoWindow({
        content: ''
    });
    for (var i = 0; i < markers.length; i++) {
      // obtain the attribues of each marker
      var lat = parseFloat(markers[i].getAttribute("Field4"));
      var lng = parseFloat(markers[i].getAttribute("Field6"));
      var marker = new google.maps.Marker({
            position : new google.maps.LatLng(lat, lng),
            map: map,
            title:"This is a marker"
        });     
        // add an event listener for this marker
        google.maps.event.addListener(marker , 'click', function() {
        // assuming you have some content in a field called Field123
                infowindow.setContent(markers[i].getAttribute("Field123"));
                infowindow.open(map, this);
        });
      stack.push(marker);
    }
    var mc = new MarkerClusterer(map,stack);
  });