Instructions for creating a Google Maps overlay that hovers above the zoom bar

Let me share my workaround for creating an overlay and dealing with z-index issues when using Google Maps API. The problem arises when dragging the map and the overlay ends up behind control elements like the zoom bar. No matter how much I adjust the z-index, it doesn't work as expected due to the structure of the parent elements having lower z-index values. If I increase the z-index of the parents, the controls end up hidden under the map itself. This is because the overlay and controls are in separate divs each with their own z-index.

<html>
<head>
    <style type="text/css">
        #map {
            width: 600px;
            height: 500px;
        }
    </style>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var map;
        TestOverlay.prototype = new google.maps.OverlayView();
        /** @constructor */
        function TestOverlay(map) {
            this.setMap(map);
        }

        /**
         * onAdd is called when the map's panes are ready and the overlay has been
         * added to the map.
         */
        TestOverlay.prototype.onAdd = function() {
            var div = document.createElement('div');
            div.innerHTML = "I want the zoombar to go behind me.";
            div.style.backgroundColor = "white";
            div.style.position = "relative";
            div.style.fontSize = "20px";
            // Add the element to the "overlayLayer" pane.
            var panes = this.getPanes();
            panes.overlayLayer.appendChild(div);
        };

        TestOverlay.prototype.draw = function() {
        };

        // The onRemove() method will be called automatically from the API if
        // we ever set the overlay's map property to 'null'.
        TestOverlay.prototype.onRemove = function() {
        };
        function init() {
            var mapCenter = new google.maps.LatLng(-35.397, 150.644);
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 8,
                center: mapCenter
            });
            new TestOverlay(map);
        }
        google.maps.event.addDomListener(window, 'load', init);
    </script>
</head>
<body>
    <div id="map"></div>
</body>

I've come up with a somewhat hacky solution to handle the overlay issue with the controls while using the Google Maps API. Here's how I managed to make it work:

<html>
    <head>
        <style type="text/css">
            #map {
                width: 600px;
                height: 500px;
            }
        </style>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript">
            var map;
            var ol;
            TestOverlay.prototype = new google.maps.OverlayView();
            /** @constructor */
            function TestOverlay(map) {
                this.setMap(map);
            }

            /**
             * onAdd is called when the map's panes are ready and the overlay has been
             * added to the map.
             */
            TestOverlay.prototype.onAdd = function() {
                var div = document.createElement('div');
                div.id = "dummy";
                // Add the element to the "overlayLayer" pane.
                var panes = this.getPanes();
                panes.overlayMouseTarget.appendChild(div);
            };

            TestOverlay.prototype.draw = function() {
            };

            // The onRemove() method will be called automatically from the API if
            // we ever set the overlay's map property to 'null'.
            TestOverlay.prototype.onRemove = function() {
            };
            function init() {
                var mapCenter = new google.maps.LatLng(-35.397, 150.644);
                map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 8,
                    center: mapCenter
                });
                google.maps.event.addListenerOnce(map, 'idle', function() {
                    var div = document.createElement('div');
                    div.id = "overlay";
                    div.className = "gmnoprint";
                    div.innerHTML = "Yes, the controls go behind me ;)";
                    div.style = "background-color:white;z-index:100000000000;position:absolute";
                    $(".gm-style").append(div);
                });
                google.maps.event.addListener(map, 'drag', function() {

                    $("#overlay").css("left", $("#dummy").parent().parent().parent().css("left"))
                    $("#overlay").css("top", $("#dummy").parent().parent().parent().css("top"))
                });
                ol = new TestOverlay(map);
            }
            google.maps.event.addDomListener(window, 'load', init);
        </script>
    </head>
    <body>
        <div id="map"></div>
    </body>
</html>

To overcome the z-index issue, I created a dummy overlay placed at the same level as the controls, and used the jQuery library to dynamically position my overlay div based on the dummy div during the drag event. It may not be the most elegant solution but it gets the job done!

Answer №1

It has come to my understanding that custom overlays consist of 6 layers:

  1. mapPane
  2. overlayLayer
  3. overlayShadow
  4. overlayImage
  5. floatShadow
  6. overlayMouseTarget
  7. floatPane

Regrettably, the design does not allow for overlays to be placed on top of controls, as it could potentially render the map ineffective.

If you haven't done so already, I recommend checking out the developers' website for information on custom overlays. There might be a solution available that hasn't been discovered yet.

https://developers.google.com/maps/documentation/javascript/customoverlays#hide_show

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Jquery's closest technique to target a class located outside of the parent div

I am having trouble accessing a class outside of its parent div $(document).on('click', '.delete_photo', function(event){ var del_id = $(this).attr('id'); $.ajax({ type:'POST', cache: false, url:&apo ...

Using jQuery to select the third element from every group of six

I am attempting to select the 3rd .foo element out of every set of 6. To clarify, here is a brief example: 1 2 3 (this) 4 5 6 1 2 3 (this) 4 5 6 and so on... So far, I have only managed to target every 3rd element, which is not exactly what I need beca ...

Ensuring JS consistently monitors changes in value

Is there an equivalent of (void update) in Unity that is called every frame in Web Development (using JavaScript)? "I want it to continuously check if certain values have changed and then update them accordingly." let governmentprice = parseFloat(select ...

Rotate the circular border in a clockwise direction when clicked

I have successfully designed a heart icon using SVG that can be filled with color upon clicking. Now, I am looking to add an outer circle animation that rotates clockwise around the heart as it is being created. Currently, the circle only spins in the code ...

Limiting the size of images within a specific section using CSS

When using CSS, I know how to resize images with the code snippets below: img {width:100%; height: auto; } img {max-width: 600px} While this method works effectively, it applies to every image on the page. What I really need is for some images to be ...

What is the term for the visual display of a product through a photo gallery or slideshow?

Can someone help me identify what the item I'm looking for? You can click on the link to see an example of the product: sephora product example What is the technical term for the section that contains product pictures and slides? Are there any librar ...

Completely place one element with respect to its sibling element

I am currently facing a situation where I have implemented a Navigation bar wrapper that reveals an overlay across the entire page when clicked. In order for this setup to function correctly, all the elements that the overlay covers must be sibling elemen ...

Error 403 with Firefox on Font Loading for CodeIgniter Website

Utilizing font-face in CSS to integrate custom fonts onto a web application being developed with CodeIgniter. This is the CSS code: @font-face { font-family: 'UniversLT-UltraCondensed'; src: url('../Fonts/LTUnivers/universlt59ultrac ...

Header width does not fully occupy the available space

I'm working on a table that includes a div element for adding a header to it. <div class="table-responsive"> <table class="table"gt; <thead> <div class="topPic"><span><i class="fa fa-tree fa-2x"></i>< ...

Prevent Text Overflow in CSS, Material-UI, and React styling efforts

I am facing an issue where the cardTitle is overlapping the Card when the text is too long. Currently, I am manually setting a static maxWidth, but I would prefer for it to adjust based on the size of the Card. Is there a way to achieve this dynamically? ...

Troubleshooting issues with adding child elements in JavaScript

Styling with CSS: ul #allposts li{ height: 50px; width: 50px; background-color: yellow; border: 1px solid yellow; } Creating HTML structure: <ul id = "allposts"></ul> Adding JavaScript functionality: var container = documen ...

Emails cannot display CSS styles

I'm experiencing a problem with my email messages not reading the CSS styles, regardless of whether I use inline CSS or not. I have tried everything but still can't figure out the issue. Can anyone assist me? $firstname = $row['firstname&ap ...

What is the best way to determine font size based on the width of the

In order to ensure that certain elements on my page do not exceed specific widths, it is crucial for me to use a monospaced font. This will limit the horizontal "stride" between characters to stay within a specified threshold. However, simply setting the ...

Various settings in JQuery UI Slider

Does anyone know how to customize the steps in a jQuery UI slider? I want to set specific values as steps, like 0, 1200, 2000, 2200, and 3000. Any suggestions on how to achieve this? const rangeSliderInit = () => { const valueArray = [0, 400, 1000, 1 ...

Bootstrap 3 with a dual sidebar layout positioned on the left side

I am currently working on a project where I need to create a fluid layout with a left sidebar. However, now I would like to add an expandable sidebar next to the existing one. Below is an image to illustrate my goal: Initially, the left sidebar contains a ...

jQuery "slide" animation without using <br>

I've been working on a website that incorporates the jQuery "Slide" effect. I have implemented this effect multiple times, using it on 3 different div tags. Each line consists of one "Dynamic" div tag (the moving one) and one "Static" div tag (the tri ...

How do I make the YouTube Grid Gallery player show up in a pop-up window?

I have been experimenting with the following code: <head> <script type="text/javascript" src="http://swfobject.googlecode.com/svn/trunk/swfobject/swfobject.js"></script> <script type="text/javascript"> function loadVideo(playerUrl, ...

The $mdSticky feature in AngularJS Material seems to be malfunctioning

It's been a challenge for me to get the md-toolbar to stay in the top position. I decided to create a custom directive using the $mdSticky service in AngularJS. var app=angular.module('app',['ngMaterial']); app.controller(&apos ...

I designed my radio buttons to ensure they cannot be selected multiple times

I have developed a basic React custom controlled radio button: export const Radio: FC<RadioProps> = ({ label, checked, onChange, disabled, }) => { const id = useId(); return ( <div> <label htmlFor={id} ...

Designs for an HTML5 Cheeseburger navigation interface

I've been struggling to implement a functional and visually appealing hamburger menu on my website. The challenge lies in integrating the menu seamlessly into the page rather than having it just pop up abruptly. Despite trying various webkit tools, I ...