最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps InfoWindow Z-Index - Stack Overflow

programmeradmin5浏览0评论

Is it possible for an InfoWindow to popup over an element which has a higher Z-Index than the google map?

So lets say the google map has a z-index of Zero. The element has a z-index of 100. Clicking a marker displays an InfoWindow in the same region as the element. How do we get this InfoWindow to hover over the element and not under it?

I have tried setting the InfoWindow's z-Index to a higher value, but this does not appear to do anything.

I've put together a demo below demonstrating the issue. Any assistance would be appreciated!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd"> 

<head>
    <title>Infowindow Z-Index Test</title>

    <script language="Javascript" type="text/javascript" src=".6.4/jquery.min.js"></script>                   
    <script src="" type="text/javascript"></script>          

    <script language="Javascript" type="text/javascript">
        var coords = [[35.50733, 35.12768, 3]]; //, [-23.806549, 133.96059, 13], [68.169002, -134.425964, 10], [69.529715, -132.109222, 10], [80.231047, -22.730713, 7], [35.108675, -117.961578, 12]];
        var map;            

        function mapChoose(coords) {
            var current = coords[Math.floor(Math.random() * (coords.length))];
            //mapShow(current[0], current[1], current[2], document.getElementById('map'));
            mapShow(current[0], current[1], current[2], document.getElementById('map_canvas'));
        };

        function initialize() {

            mapChoose(coords);
        }

        function mapShow(lat, lng, zoom, div) {
            var myOptions = {
                'zoom': zoom,
                'center': new google.maps.LatLng(lat, lng),
                'mapTypeId': google.maps.MapTypeId.SATELLITE,
                'disableDefaultUI': true,
                'scrollwheel' : false,
                'draggable': false
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            // markers
            // load markers - us
            var marker3 = new google.maps.Marker({
                position: new google.maps.LatLng(40.71435, -74.00597),
                map: map,
                icon: '.png',
                draggable: true
            });
            marker3.setTitle("New York");                


            // info windows                
            var contentString = $("#divContentForInfoWindow").html();
            $("#divContentForInfoWindow").html('');


            infowindow3 = new google.maps.InfoWindow({
                content: contentString, zIndex: 5000 // this ZIndex property doesnt appear to do anything.
            });

            google.maps.event.addListener(marker3, 'click', function () {
                infowindow3.open(map, marker3);

            });

        };

    </script>

</head>


<body onload="initialize()" onunload="GUnload()">

    <!-- Google canvas element | Z-Index of 0 -->
    <div id="map_canvas" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0; z-index: 0;"></div>         


    <!-- Floating block of text | Z-Index of 100  -->
    <div id="FloatingBlock" style='z-index: 100;position: absolute;left: 50px;top:75px;font-size: 16px;width: 525px;text-align: left;color: #111;background-color:#fff;opacity:.80;filter:alpha(opacity=80);'>

        <b>This layer is floating above the google map because the Z-Index has been set higher. <br /></b>

        Hello world hello world hello world hello world hello world hello world hello world<br />
        hello world hello world hello world hello world hello world hello world hello world<br /> 
        Hello world hello world hello world hello world hello world hello world hello world<br /> 
        hello world hello world hello world hello world hello world hello world hello world<br /> 
        Hello world hello world hello world hello world hello world hello world hello world<br /> 
        hello world hello world hello world hello world hello world hello world hello world<br /> 

        <div class="text">

          <!-- <input type="button" id="btntest" value="Open Info Window" onclick="showInfoWindow2();" /><input type="button" id="btntest2" value="hide Info Window" onclick="hideInfoWindow2();" />-->
    </div>

    <div style="clear:both;"></div>

    </div>      
    <!-- / can delete this, not used -->

    <!-- everything IN here will appear in the google maps info window -->
    <div id="divContentForInfoWindow">       

        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />

   </div>
   <br /><br /><br />        
    <!-- / everything in here will appear in the google maps info window -->
</body>

Is it possible for an InfoWindow to popup over an element which has a higher Z-Index than the google map?

So lets say the google map has a z-index of Zero. The element has a z-index of 100. Clicking a marker displays an InfoWindow in the same region as the element. How do we get this InfoWindow to hover over the element and not under it?

I have tried setting the InfoWindow's z-Index to a higher value, but this does not appear to do anything.

I've put together a demo below demonstrating the issue. Any assistance would be appreciated!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<head>
    <title>Infowindow Z-Index Test</title>

    <script language="Javascript" type="text/javascript" src="https://ajax.googleapis./ajax/libs/jquery/1.6.4/jquery.min.js"></script>                   
    <script src="http://maps.google./maps/api/js?sensor=false" type="text/javascript"></script>          

    <script language="Javascript" type="text/javascript">
        var coords = [[35.50733, 35.12768, 3]]; //, [-23.806549, 133.96059, 13], [68.169002, -134.425964, 10], [69.529715, -132.109222, 10], [80.231047, -22.730713, 7], [35.108675, -117.961578, 12]];
        var map;            

        function mapChoose(coords) {
            var current = coords[Math.floor(Math.random() * (coords.length))];
            //mapShow(current[0], current[1], current[2], document.getElementById('map'));
            mapShow(current[0], current[1], current[2], document.getElementById('map_canvas'));
        };

        function initialize() {

            mapChoose(coords);
        }

        function mapShow(lat, lng, zoom, div) {
            var myOptions = {
                'zoom': zoom,
                'center': new google.maps.LatLng(lat, lng),
                'mapTypeId': google.maps.MapTypeId.SATELLITE,
                'disableDefaultUI': true,
                'scrollwheel' : false,
                'draggable': false
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            // markers
            // load markers - us
            var marker3 = new google.maps.Marker({
                position: new google.maps.LatLng(40.71435, -74.00597),
                map: map,
                icon: 'http://google-maps-icons.googlecode./files/kitesurfing.png',
                draggable: true
            });
            marker3.setTitle("New York");                


            // info windows                
            var contentString = $("#divContentForInfoWindow").html();
            $("#divContentForInfoWindow").html('');


            infowindow3 = new google.maps.InfoWindow({
                content: contentString, zIndex: 5000 // this ZIndex property doesnt appear to do anything.
            });

            google.maps.event.addListener(marker3, 'click', function () {
                infowindow3.open(map, marker3);

            });

        };

    </script>

</head>


<body onload="initialize()" onunload="GUnload()">

    <!-- Google canvas element | Z-Index of 0 -->
    <div id="map_canvas" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0; z-index: 0;"></div>         


    <!-- Floating block of text | Z-Index of 100  -->
    <div id="FloatingBlock" style='z-index: 100;position: absolute;left: 50px;top:75px;font-size: 16px;width: 525px;text-align: left;color: #111;background-color:#fff;opacity:.80;filter:alpha(opacity=80);'>

        <b>This layer is floating above the google map because the Z-Index has been set higher. <br /></b>

        Hello world hello world hello world hello world hello world hello world hello world<br />
        hello world hello world hello world hello world hello world hello world hello world<br /> 
        Hello world hello world hello world hello world hello world hello world hello world<br /> 
        hello world hello world hello world hello world hello world hello world hello world<br /> 
        Hello world hello world hello world hello world hello world hello world hello world<br /> 
        hello world hello world hello world hello world hello world hello world hello world<br /> 

        <div class="text">

          <!-- <input type="button" id="btntest" value="Open Info Window" onclick="showInfoWindow2();" /><input type="button" id="btntest2" value="hide Info Window" onclick="hideInfoWindow2();" />-->
    </div>

    <div style="clear:both;"></div>

    </div>      
    <!-- / can delete this, not used -->

    <!-- everything IN here will appear in the google maps info window -->
    <div id="divContentForInfoWindow">       

        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />
        This is popup content. <br />This is popup content. <br />

   </div>
   <br /><br /><br />        
    <!-- / everything in here will appear in the google maps info window -->
</body>

Share Improve this question asked Sep 7, 2012 at 10:40 TheLegendaryCopyCoderTheLegendaryCopyCoder 1,8323 gold badges26 silver badges50 bronze badges 1
  • 4 You wont actually be able to adjust the z-index the way you're trying to. CSS parent children relationships won't let you. Basically as long as the map is a lower z-index then the floating block, the info windows can never be above it with this setup. Your best bet, would be to try CustomOverlays or try to add your floating div into the map through the DOM – jassok Commented Sep 17, 2014 at 15:36
Add a ment  | 

2 Answers 2

Reset to default 10

use infowindow.setZIndex(Number);

You can use a custom control instead of outer element to place on the map. The custom control is placed above the map and when you open an InfoWindow, Google maps detects the position of the control and automatically moves and opens the InfoWindow below the control, so the both don't overlap. In this case it is better to use it instead of the custom overlay, because the overlay moves when you drag or zoom the map and the control always stays at the same position.

So, first you have to add the map and the control as div-s in your html, then to initialize the map and after that to add the control to the map like that:

var control = document.getElementById('control');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(control);

How simple is that ;)

发布评论

评论列表(0)

  1. 暂无评论