【问题标题】:Google Map Api v3, how to get list of custom InfoBoxes (custom Overlays)?Google Map Api v3,如何获取自定义信息框(自定义叠加)列表?
【发布时间】:2010-10-17 21:29:38
【问题描述】:

我使用next link 创建了自定义叠加层。

如何获取已创建的叠加层(或数组,或其他)列表?

我需要它,因为我需要关闭其中一些的能力。

【问题讨论】:

    标签: google-maps-api-3 overlay


    【解决方案1】:

    您需要跟踪您在数组中创建了哪些叠加层,如果您需要关闭其中一些叠加层,您可以在数组中找到它们并在它们上设置映射(null)或其他摆脱它们的方法(close( ) 在下面的示例中)。为数组中的标记或叠加层创建自定义 ID 非常有用,这样您就可以快速找到它们。

    这是从 Google Maps Utils 库中关闭自定义信息框的示例

    <html> 
    <head> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1/src/infobox.js"></script> 
    <script type="text/javascript"> 
    
        //this is the array to store our custom objects in 
         ibArray = [];
    
        //standard stuff
        function initialize() {
    
            var secheltLoc = new google.maps.LatLng(49.47216, -123.76307);
    
            var myMapOptions = {
                 zoom: 15
                ,center: secheltLoc
                ,mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var theMap = new google.maps.Map(document.getElementById("map_canvas"), myMapOptions);
    
    
            var marker = new google.maps.Marker({
              map: theMap
             ,position: new google.maps.LatLng(49.47216, -123.76307)
             ,visible: true
            });
    
            var boxText = document.createElement("div");
            boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
            boxText.innerHTML = "City Hall, Sechelt<br>British Columbia<br>Canada";
    
            var myOptions = {
                 content: boxText
                ,disableAutoPan: false
                ,maxWidth: 0
                ,pixelOffset: new google.maps.Size(-140, 0)         
                ,zIndex: null
                ,boxStyle: { 
                  background: "url('tipbox.gif') no-repeat"
                  ,opacity: 0.75
                  ,width: "280px"
    
                 }
                ,closeBoxMargin: "10px 2px 2px 2px"
                ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
                ,infoBoxClearance: new google.maps.Size(1, 1)
                ,isHidden: false
                ,pane: "floatPane"
                ,enableEventPropagation: false
    
            };
    
            var ib = new InfoBox(myOptions);
    
            //store your info box in the array with a custom id - there are number of ways you can index this - it really depends on what you are doing
            //you will need to all your objects that you want to close later. 
            ibArray.push({myId:"1234",box: ib});        
    
            //open the box
            ib.open(theMap, marker);
        }
    
        //close the box with an id passed to this function
        function closeBox(id){
    
            for (i=0;i<ibArray.length;i++) {
                if (ibArray[i].myId==id){
                    myBox = ibArray[i].box;
                    myBox.close();
                }
            }
        }
    </script> 
    
    </head> 
    <body onload="initialize()"> 
        <div id="map_canvas" style="width:100%; height:50%"></div> 
        <p> 
        <input type="button" value="close box with custom id" onclick="javascript:closeBox('1234')">
    </body> 
    
    </html> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多