Google Maps - Two heatmap layers
WIP
by katalin_2003
HTML
<!DOCTYPE html>
<html>
<head>
<title>Heatmaps</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="floating-panel">
<button id="toggle-heatmap">Toggle Heatmap 2</button>
<button id="change-gradient">Change gradient map 2</button>
<button id="change-radius">Change radius map 1</button>
<button id="change-opacity">Change opacity map 1</button>
</div>
<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&libraries=visualization&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;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: "Roboto", "sans-serif";
line-height: 30px;
padding-left: 10px;
}
#floating-panel {
background-color: #fff;
border: 1px solid #999;
left: 25%;
padding: 5px;
position: absolute;
top: 10px;
z-index: 5;
}
JavaScript
// This example requires the Visualization library. Include the libraries=visualization
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&libraries=visualization">
let map, heatmap, heatmap2;
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
// zoom: 6, // France
zoom: 13,
center: { lat: 37.775, lng: -122.434 },
// center: {lat: 47.11, lng: 2.37}, // France
mapTypeId: "satellite",
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
map: map,
});
heatmap2 = new google.maps.visualization.HeatmapLayer({
data: getPoints2(),
map: map,
});
// heatmap2.set("gradient2", heatmap2.get("gradient2") ? null : gradient2);
changeGradient();
document
.getElementById("toggle-heatmap")
.addEventListener("click", toggleHeatmap);
document
.getElementById("change-gradient")
.addEventListener("click", changeGradient);
document
.getElementById("change-opacity")
.addEventListener("click", changeOpacity);
document
.getElementById("change-radius")
.addEventListener("click", changeRadius);
}
function toggleHeatmap() {
// heatmap.setMap(heatmap.getMap() ? null : map);
heatmap2.setMap(heatmap2.getMap() ? null : map);
}
function changeGradient() {
const gradient = [
"rgba(0, 255, 255, 0)",
"rgba(0, 255, 255, 1)",
"rgba(0, 191, 255, 1)",
"rgba(0, 127, 255, 1)",
"rgba(0, 63, 255, 1)",
"rgba(0, 0, 255, 1)",
"rgba(0, 0, 223, 1)",
"rgba(0, 0, 191, 1)",
"rgba(0, 0, 159, 1)",
"rgba(0, 0, 127, 1)",
"rgba(63, 0, 91, 1)",
"rgba(127, 0, 63, 1)",
"rgba(191, 0, 31, 1)",
"rgba(255, 0, 0, 1)",
];
heatmap2.set("gradient", heatmap2.get("gradient") ? null : gradient);
}
function changeRadius() {
heatmap.set("radius", heatmap.get("radius") ? null : 20);
}
function changeOpacity()...