JSFiddle - React, Tailwind, and code Playground
by Wolf
HTML
<!DOCTYPE html>https://jsfiddle.net/user/login/
<html>
<head>
<title>Data Layer: Dynamic Styling</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="map"></div>
<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly&channel=2"
async
></script>
</body>
</html>
CSS
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
JavaScript
let map;
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
zoom: 4,
center: { lat: -28, lng: 137 },
});
// Load GeoJSON.
map.data.loadGeoJson(
"https://storage.googleapis.com/mapsdevsite/json/google.json"
);
let hide = 'g';
// Color each letter gray. Change the color when the isColorful property
// is set to true.
map.data.setStyle((feature) => {
let visible = true;
if (feature.getProperty("letter") == hide) {
visible = false;
}
return /** @type {!google.maps.Data.StyleOptions} */ {
strokeWeight: 2,
visible: visible,
};
});
// When the user clicks, set 'isColorful', changing the color of the letters.
map.data.addListener("click", (event) => {
event.feature.setProperty("isColorful", !event.feature.getProperty("isColorful"));
});
// When the user hovers, tempt them to click by outlining the letters.
// Call revertStyle() to remove all overrides. This will use the style rules
// defined in the function passed to setStyle()
map.data.addListener("mouseover", (event) => {
map.data.revertStyle();
map.data.overrideStyle(event.feature, { strokeWeight: 8 });
});
map.data.addListener("mouseout", (event) => {
map.data.revertStyle();
});
}