简体   繁体   English

打开其他时如何关闭信息多头?

[英]How to close info bull when opening other?

I have displayed my positions in the map like this: 我已经在地图上显示了自己的位置,如下所示:

<script type="text/javascript" src="https://maps-api-ssl.google.com/maps/api/js?v=3&amp;sensor=true"></script>
<script type="text/javascript">
    var map;
    var markers = new Array();
    var infos = new Array();

    jQuery(document).ready(function(){
        var mapOptions = {
                zoom: 2,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        {% for position in positions %}
            var address = '{{ position.map_position }}';
            geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var content_html  = '<div>';
                    content_html += '<label style="font-weight:bold;">country name : </label>{{ position.country.country_name }}<br/>';
                    content_html += '<label style="font-weight:bold;">city name : </label>{{ position.city.city_name }}<br/>';
                    {% if position.related > 1 %}
                    content_html += '<a style="color:black;" href="Mylink">My link</a>';
                    {% endif %}

                    if(jQuery.inArray('{{ position.map_position }}', markers) == -1){
                        map.setCenter(results[0].geometry.location);
                        var marker = new google.maps.Marker({
                            map: map,
                            icon: '{{position.icon}}',
                            position: results[0].geometry.location
                        });
                        markers.push('{{ position.map_position }}');
                        var infowindow = new google.maps.InfoWindow({
                            content: content_html
                        });
                        infos.push(infowindow);
                        google.maps.event.addListener(marker, 'click', function() {
                            infowindow.open(map,marker);
                        });
                    }
                    else {
                        var infowindow = infos[jQuery.inArray('{{ position.map_position }}', markers)];
                        infowindow.setContent(infowindow.getContent()+'<hr>'+content_html);
                    }
                }
            });
        {% endfor %}
    });
</script>

I displayed positions in the map but if i had more than 2 markers i can open info bull of all of them. 我在地图上显示了位置,但是如果我有两个以上的标记,则可以打开所有这些标记的信息。 How can i open just one info bull at the same time ? 我怎样才能同时打开一只信息牛? (if i open one info bull, all others stay closed) (如果我打开一头信息牛,所有其他人保持关闭)

Instead of creating several infos as Array. 而不是创建多个infos作为数组。 Create a single infowindow in your initialization. 在初始化中创建一个infowindow In your event/listener where you want to open the infowindow , you'd set the content and open the infowindow on the marker/location on the map. 在要打开infowindow的事件/侦听器中,您将设置内容并在infowindow上的标记/位置上打开infowindow

//Initialize infowinow
var infowindow = new google.maps.InfoWindow({
       content: ''
 });

function add_marker(point, name, content_html)
{
   var marker = new google.maps.Marker({
      map: map,
      position: point,
      dragable: false,
      clickable: true,
      name: name
   });
   marker.content_html = content_html;
   google.maps.event.addListener(marker, 'click', function()
   {
      infowindow.setContent(marker.content_html);
      infowindow.open(map, marker);
   });
   return marker;
};

I would like to share solution to help for the next consultation...I have just replaced this: 我想分享解决方案以帮助下一次咨询...我已经替换了这个:

google.maps.event.addListener(marker, 'click', function() {
     infowindow.open(map,marker);
});

with this: 有了这个:

google.maps.event.addListener(marker, 'click', function() {
    // Add a loop for closing all infowindows and after it we open the merkered one
    for(var i=0; i < infos.length; i++){
        infos[i].close();
    }
    infowindow.open(map,marker);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM