Styled Map Selection
Sample code for Google Maps Platform JavaScript API author(s): Justin Poehnelt
by Cybersoulja
HTML
<!DOCTYPE html>
<!--
@license
Copyright 2019 Google LLC. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
-->
<html>
<head>
<title>Styled Map Selection</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="style-selector-control" class="map-control">
<select id="style-selector" class="selector-control">
<option value="default">Default</option>
<option value="silver">Silver</option>
<option value="night">Night mode</option>
<option value="retro" selected="selected">Retro</option>
<option value="hiding">Hide features</option>
</select>
</div>
<div id="map"></div>
<!--
The `defer` attribute causes the callback to execute after the full HTML
document has been parsed. For non-blocking uses, avoiding race conditions,
and consistent behavior across browsers, consider loading using Promises
with https://www.npmjs.com/package/@googlemaps/js-api-loader.
-->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly" defer></script>
</body>
</html>
CSS
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/*
* 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;
}
.map-control {
background-color: #fff;
border: 1px solid #ccc;
box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
font-family: "Roboto", "sans-serif";
margin: 10px;
/* Hide the control initially, to prevent it from appearing
before the map loads. */
display: none;
}
/* Display the control once it is inside the map. */
#map .map-control {
display: block;
}
.selector-control {
font-size: 14px;
line-height: 30px;
padding-left: 5px;
padding-right: 5px;
}
JavaScript
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
let map;
function initMap() {
// Create the map with no initial style specified.
// It therefore has default styling.
map = new google.maps.Map(document.getElementById("map"), {
center: {
lat: -33.86,
lng: 151.209
},
zoom: 13,
mapTypeControl: false,
});
// Add a style-selector control to the map.
const styleControl = document.getElementById("style-selector-control");
map.controls[google.maps.ControlPosition.TOP_LEFT].push(styleControl);
// Set the map's style to the initial value of the selector.
const styleSelector = document.getElementById("style-selector");
map.setOptions({
styles: styles[styleSelector.value]
});
// Apply new JSON when the user selects a different style.
styleSelector.addEventListener("change", () => {
map.setOptions({
styles: styles[styleSelector.value]
});
});
}
const styles = {
default: [],
silver: [{
elementType: "geometry",
stylers: [{
color: "#f5f5f5"
}],
},
{
elementType: "labels.icon",
stylers: [{
visibility: "off"
}],
},
{
elementType: "labels.text.fill",
stylers: [{
color: "#616161"
}],
},
{
elementType: "labels.text.stroke",
stylers: [{
color: "#f5f5f5"
}],
},
{
featureType: "administrative.land_parcel",
elementType: "labels.text.fill",
stylers: [{
color: "#bdbdbd"
}],
},
{
featureType: "poi",
elementType: "geometry",
stylers: [{
color: "#eeeeee"
}],
},
{
featureType: "poi",
elementType: "labels.text.fill",
stylers: [{
color: "#757575"
}],
},
{
featureType: "poi.park",
elementType: "geometry",
stylers: [{
color: "#e5e5e5"
}],
},
{
featureType:...