close InfoWindow before open another

前端 未结 3 1164
终归单人心
终归单人心 2020-11-27 23:48

I have a problem with InfoWindow. I have an ajax function that retrieves data via JSON, but I can not get close InfoWindow automatically when you open another. My code is th

相关标签:
3条回答
  • 2020-11-28 00:17

    The suggestion is only create one infowindow (in the global scope), reuse it and change its contents when a marker is clicked, close it if the user clicks on the map.

    code snippet (removes the dependency on the AJAX call):

    // global variables
    var map;
    var markers = [];
    var infowindow = new google.maps.InfoWindow();
    
    function initialize() {
      var mapOptions = {
        center: new google.maps.LatLng(44.49423583832911, 11.346244544982937),
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("mappa_locali"), mapOptions);
      var json = JSON.parse(data);
      for (var i = 0; i < json.length; i++) {
        point = new google.maps.LatLng(json[i].latitudine, json[i].longitudine);
        var infowindowHtml = '<a href="./dettaglioLocale.php?id_loc=' + json[i].id_locale + '">' + json[i].nome_locale + '</a><br>' + json[i].address;
        addMarkerz(point, infowindowHtml);
      }
    }
    
    function addMarkerz(point, infowindowHtml) {
      var marker = new google.maps.Marker({
        position: point,
        map: map
      });
      google.maps.event.addListener(marker, 'mouseover', infoCallback(infowindowHtml, marker));
      markers.push(marker);
    }
    
    function infoCallback(infowindowHtml, marker) {
      return function() {
        infowindow.close();
        // update the content of the infowindow before opening it
        infowindow.setContent(infowindowHtml)
        infowindow.open(map, marker);
    
      };
    }
    
    
    var data = JSON.stringify([{
      latitudine: 44.494887,
      longitudine: 11.3426163,
      id_locale: "0",
      nome_locale: "Bologna",
      address: "Bologna, Italy"
    }, {
      latitudine: 44.4946615,
      longitudine: 11.314634999999953,
      id_locale: "0",
      nome_locale: "Quartiere Saragozza",
      address: "Quartiere Saragozza, Bologna, Italy"
    }]);
    
    
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #mappa_locali {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <div id="mappa_locali"></div>

    0 讨论(0)
  • 2020-11-28 00:21

    I think I have a simple solution. I just save the last open mark. On the next click first of all I close the last mark.

    var markers = [];
    var infowindows = [];
    
    function addMarkes(facilityID) {
    
            var bounds = new google.maps.LatLngBounds();
    
            for (var i = 0; i < variants.length; i++) {
    
                var v = variants[i];
    
                var position = new google.maps.LatLng(v.Lat, v.Long);
    
                bounds.extend(position);
    
                markers[i] = new google.maps.Marker({
                    position: position,
                    title: v.Title,
                    map: map,
                    animation: google.maps.Animation.DROP,
                    icon: v.Icon,
                    infoWindowIndex: i //synchronize with infoWindows
                });
    
                var localContent = '<div><span class="address">' + v.Address + '</span></div>';
    
                infowindows[i] = new google.maps.InfoWindow({
                    content: localContent
                });
    
                //Just for multiple marks. 
                if (variants.length > 0) {
                    var lastOpen = -1; //Save the last open mark
                    google.maps.event.addListener(markers[i], 'click', function (innerKey) {
                        return function () {
                            //Close the last mark.
                            if (lastOpen > -1) {
                                infowindows[lastOpen].close();
                            }
                            infowindows[innerKey].open(map, markers[innerKey]);
                            lastOpen = innerKey;
                        }
                    }(i));
                }
            }
    
            map.fitBounds(bounds);
    
        }
    
    0 讨论(0)
  • 2020-11-28 00:31
    function infoCallback(infowindow, marker) { 
      return function() {
         infowindow.close();  
        infowindow.open(map, marker);
    
      };
    }
    

    should be changed to

    function infoCallback(infowindow, marker) { 
        return function() {
            //Close active window if exists
            if (activeWindow != null) {
                activeWindow.close();
            }
            //Open new window 
            infowindow.open(map,marker); 
            //Store new window in global variable 
            activeWindow = infowindow; 
        };
    }
    

    and declare activeWindow as a global variable in your .js file as var activeWindow.

    0 讨论(0)
提交回复
热议问题