Adjusting the z-index of a marker on Google Maps' data layer

While creating a Google Map, I encountered an issue where overlapping circles needed their z-index changed upon hover to bring them to the front. I found a solution for markers in this link: changing z index of marker on hover to make it visible. However, I wanted to apply this functionality to points created by a data layer. Here is a sample of my code on JSFiddle:

http://jsfiddle.net/8cs97z8h/1/


var json = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [-98.344139,28.629211]
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [-98.341263,28.629228]
            }
        }, 
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [-98.3412, 28.629]
            }
        },
    ]
};

var map = new google.maps.Map(document.getElementById('map-canvas'), {
    zoom: 12,
    center: new google.maps.LatLng(28.666767, -98.367298),
});

map.data.addGeoJson(json);
map.data.setStyle(styleFeature);

function styleFeature(feature) {
    return {
        icon: {
            path: google.maps.SymbolPath.CIRCLE,
            strokeWeight: 2,
            strokeColor: 'white',
            fillColor: 'blue',
            fillOpacity: 1.0,
            scale: 7
        }
    };
}

Answer №1

To achieve this, you can utilize the overrideStyle method.

Create a variable to store the zIndex value and use it in the setStyle function.

    var zIndex=1;
    //Setting style for markers circles.
    function styleFeature(feature) {
       return {
            icon: {
                path: google.maps.SymbolPath.CIRCLE,
                strokeWeight: 2,
                strokeColor: 'white',
                fillColor: 'blue',
                fillOpacity: 1.0,
                scale: 7
            },
             zIndex:zIndex
        };
    }

Next, add a mouseover-listener to increment the zIndex and apply it to the feature:

map.data.addListener('mouseover', function(event) {
       map.data.overrideStyle(event.feature, {zIndex: ++zIndex});
});

Check out the demo here: http://jsfiddle.net/8cs97z8h/6/

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

Creating Animated Rotation Resets Using CSS Keyframes

My latest creation involves an animated display of leaves "waving" gracefully. These leaves come with a default rotate value from tailwind: @keyframes growSize { 0% { transform: rotate(0deg); max-height: 0; } 25% { max-height: 9999px; ...

Issue with JQuery integration in ListView components

My goal is to restrict certain textboxes to accept only numbers using jQuery. However, the code snippet I have below is not working when applied to a textbox inside a ListView or on another asp.net page. $(document).ready(function () { $("#<%= ...

The jQuery scrollTop feature seems to be malfunctioning

My code is causing an error that I don't understand. Any help would be appreciated... I'm getting the following error message: Property does not exist on type '.js--section-plan'.ts(2339) when I hover over the offset() in vscode $(&apos ...

Managing absent fields in ajax request using jQuery

Utilizing ajax to retrieve data from YouTube videos presents a challenge when some videos include geolocation information while others do not. This leads to an error message such as: Uncaught TypeError: Cannot read property 'latitude' of undefine ...

Dynamic Filtering of HTML Dropdown Options Based on Another Dropdown Selection

I am facing an issue with multiple HTML dropdowns. My requirement is that upon selecting an option from one dropdown, the next dropdown should get automatically populated. The data to populate these dropdowns is fetched from a database using SQL statements ...

Is there a method in Node.js Express/Connect to manipulate the session to have an indefinite duration?

This is my current approach: app.use(express.session({ cookie:{domain:"."+settings.c.SITE_DOMAIN}, secret:'abc', store: redis_store, })); Upon checking my redis and running TTL se ...

Display HTML content in a modal dialog box without parsing it

I'm currently working on a website showcasing various HTML and CSS spinners and loaders. Each example opens a modal window upon click, where I aim to display the corresponding code for that spinner so users can easily copy and implement it in their ow ...

Change the content of a text field in real-time using Angular.js

Looking to make a directive where clicking on a field allows for editing. I want to be able to specify the type of input, such as "textarea" instead of just an input text field. Currently using ng-if for this validation. https://i.sstatic.net/NAPbj.jpg H ...

Is there a solution for overflow indicators in combination with a FlexBox column layout?

To clarify this question, we do not have to provide a CSS-only solution. We are open to using JavaScript in our data-driven project. Hello! Thank you for visiting. In summary, we want to enhance Flexbox column layout by breaking the content at specific ...

Misalignment of Font-awesome icons in the footer section

I've implemented a footer on my website. My goal is to have the icons centered both vertically and horizontally, with the colored area: Always positioned at the bottom of the screen No taller than the icons themselves Here's the code snippet: ...

Executing an AJAX request when the window is closed using jQuery

I have a clear question: how can I trigger an ajax call when the user clicks on the browser close button, but before actually closing the window? Are there any possible solutions for this specific scenario? I do not want any confirmation boxes - just a w ...

Combining a collection of objects to create comprehensive dataset

Below are the scripts needed to create the intended submission to PHP: index.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>test</title> </head> <body> ...

To make the label font size larger when it appears on hover in chart.js

I have been attempting to increase the font size of labels that appear on hover in chart.js. I am also trying to customize the text of the label, but so far, I have not been successful in increasing its font size. var myPieChart = new Chart(ctxP, { ...

Utilize various CSS styles for text wrapping

I'm struggling to figure out where to begin with this problem. My goal is to create a three-column row that can wrap below each other if they cannot fit horizontally. The height of the row should adjust to accommodate the items while maintaining a fix ...

JavaScript - Calculate the streak of consecutive work days

Consider this array of plans: [ { _id: "1", project_id: "1", day: "2021-03-02" }, { _id: "2", project_id: "1", day: "2021-03-01" }, { _id: "3", project_id: "1", day: "2021-03-03" }, { _id: "4", project_id: "2", day: "2021-03-01" ...

Challenges with responsive layout of flex items

Utilizing Tailwind CSS, I have designed this div element: https://i.sstatic.net/FDloP.png However, an issue arises when the div is resized: https://i.sstatic.net/Sq309.png Using white-space: nowrap causes it to extend beyond the div boundaries. https://i ...

The assigned type does not match the type 'IntrinsicAttributes & { children?: ReactNode; }'. This property is not assignable

I have been struggling to resolve this issue, but unfortunately, I have not found a successful solution yet. The error message I am encountering is: Type '{ mailData: mailSendProps; }' is causing an issue as it is not compatible with type &apos ...

JavaScript - Marking selected text: What are the options?

I am looking to implement a feature in JavaScript (without jQuery) that will allow me to highlight selected text with control points or markers (left and right). These markers should function similar to those on mobile phones, allowing me to extend the sel ...

Utilizing NodeJS on an alternative drive other than the system drive

Situation: On my Windows 8 x64 machine, I have NodeJS installed on the C drive. QUnit is also installed globally using the command npm install qunit -g. The issue at hand: When I try to reference QUnit by using var q = require('qunit'); while ...

Performing an AJAX request when leaving the page

I'm currently developing a website with various links to external sites. My goal is to send an ajax call to notify the server when a user clicks on one of these links and navigates away from the page. I've implemented an alert on link click, whic ...