使用 MarkerClusterer 标记上的 InfoWindow

发布于 2024-12-07 02:45:34 字数 1255 浏览 1 评论 0原文

这是我的 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>

This is my html code. I've try anything to add an infowindow on the markers but it don't wanna work. My data is loading from the "Alle_Ortswahlen.page1.xml" file.
Do anyone have an idea how can I add infoWindow to each marker?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-12-14 02:45:34

在 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 开发中心:使用闭包

,您的代码应如下所示:

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

Before the for cycle, make an empty infowindow object.

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

Than in the for cycle, after the marker, add an event listener, like this:

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

There is some closure magic happening when passing the callback argument to the addListener method. If you are not familiar with it, take a look at here:

Mozilla Dev Center: Working with Closures

So, your code should look something like this:

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);
            });
        }
隱形的亼 2024-12-14 02:45:34

因此,您需要做的是在 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);
  });

So what you need to do is add some code, inside your for-loop, associating an infowindow onclick event handler with each marker. I'm assuming you only want to have 1 infowindow showing at a time, i.e. you click on a marker, the infowindow appears with relevant content. If you then click on another marker, the first infowindow disappears, and a new one reappears attached to the other marker. Rather than having multiple infowindows all visible at the same time.

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);
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文