close InfoWindow before open another

前端 未结 3 1181
终归单人心
终归单人心 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: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.

提交回复
热议问题