Example
The example below demonstrate the code for creating a Heatmap on a map which is stored at client side.
[c]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Heatmaps Layers</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#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;
}
</style>
</head>
<body>
<div id="floating-panel">
<button onclick="toggleHeatmap()">Toggle Heatmap</button>
<button onclick="changeGradient()">Change gradient</button>
<button onclick="changeRadius()">Change radius</button>
<button onclick="changeOpacity()">Change opacity</button>
</div>
<div id="map"></div>
<script>
var map, heatmap;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {lat: 17.3700, lng: 78.4800},
mapTypeId: google.maps.MapTypeId.SATELLITE
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
map: map
});
}
function toggleHeatmap() {
heatmap.setMap(heatmap.getMap() ? null : map);
}
function changeGradient() {
var 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)'
]
heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);
}
function changeRadius() {
heatmap.set('radius', heatmap.get('radius') ? null : 20);
}
function changeOpacity() {
heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2);
}
// Heatmap data: 100 Points
function getPoints() {
return [
new google.maps.LatLng(17.3710, 78.4850),
new google.maps.LatLng(17.3730, 78.4900),
new google.maps.LatLng(17.3750, 78.5000),
new google.maps.LatLng(17.3770, 78.5150),
new google.maps.LatLng(17.3790, 78.5200),
new google.maps.LatLng(17.3810, 78.5300),
new google.maps.LatLng(17.3830, 78.5350),
new google.maps.LatLng(17.3840, 78.5400),
new google.maps.LatLng(17.3865, 78.5500),
new google.maps.LatLng(17.3875, 78.5800),
new google.maps.LatLng(17.3880, 78.5850),
new google.maps.LatLng(17.3890, 78.5900),
new google.maps.LatLng(17.3900, 78.6200),
new google.maps.LatLng(17.3910, 78.6500),
new google.maps.LatLng(17.3930, 78.6800),
new google.maps.LatLng(17.3940, 78.6700),
new google.maps.LatLng(17.3956, 78.7000),
new google.maps.LatLng(17.3980, 78.7100),
new google.maps.LatLng(17.3990, 78.7300),
new google.maps.LatLng(17.4000, 78.7540),
new google.maps.LatLng(17.4020, 78.7600),
new google.maps.LatLng(17.4040, 78.7750),
new google.maps.LatLng(17.4060, 78.7810),
new google.maps.LatLng(17.4080, 78.7850),
new google.maps.LatLng(17.4090, 78.7875),
new google.maps.LatLng(17.4110, 78.7900),
new google.maps.LatLng(17.4330, 78.7950),
new google.maps.LatLng(17.4400, 78.7990),
new google.maps.LatLng(17.4500, 78.8110),
new google.maps.LatLng(17.4550, 78.8400),
new google.maps.LatLng(17.4600, 78.8550),
new google.maps.LatLng(17.4640, 78.8560),
new google.maps.LatLng(17.4700, 78.8610),
new google.maps.LatLng(17.4800, 78.8650),
new google.maps.LatLng(17.4850, 78.8700),
new google.maps.LatLng(17.4900, 78.8800),
new google.maps.LatLng(17.6000, 78.8890),
new google.maps.LatLng(17.6500, 78.8900),
new google.maps.LatLng(17.6550, 78.9000),
new google.maps.LatLng(17.6660, 78.9110),
new google.maps.LatLng(17.6700, 78.11000),
new google.maps.LatLng(17.6800, 78.11200),
new google.maps.LatLng(17.6850, 78.11500),
new google.maps.LatLng(17.6855, 78.11200),
new google.maps.LatLng(17.6900, 78.11250),
new google.maps.LatLng(17.6985, 78.11350),
new google.maps.LatLng(17.8000, 78.11420),
new google.maps.LatLng(17.8220, 78.11450),
new google.maps.LatLng(17.8440, 78.11500),
new google.maps.LatLng(17.8500, 78.11600),
new google.maps.LatLng(17.8600, 78.11650),
new google.maps.LatLng(17.9000, 78.11700),
new google.maps.LatLng(17.9110, 78.11720),
new google.maps.LatLng(17.9220, 78.11740),
new google.maps.LatLng(17.9350, 78.11820),
new google.maps.LatLng(17.9360, 78.11835),
new google.maps.LatLng(17.9440, 78.11850),
new google.maps.LatLng(17.9760, 78.11860),
new google.maps.LatLng(17.9890, 78.11870),
new google.maps.LatLng(17.9940, 78.11900),
new google.maps.LatLng(17.9990, 78.11910),
new google.maps.LatLng(17.10000, 78.11940),
new google.maps.LatLng(17.10300, 78.11960),
new google.maps.LatLng(17.10540, 78.11980),
new google.maps.LatLng(17.10290, 78.12000),
new google.maps.LatLng(17.10450, 78.12250),
new google.maps.LatLng(17.10650, 78.12400),
new google.maps.LatLng(17.10780, 78.12550),
new google.maps.LatLng(17.10800, 78.12600),
new google.maps.LatLng(17.10990, 78.12780),
new google.maps.LatLng(17.13000, 78.15100),
new google.maps.LatLng(17.13220, 78.15350),
new google.maps.LatLng(17.13300, 78.15355),
new google.maps.LatLng(17.13440, 78.15360),
new google.maps.LatLng(17.13550, 78.15465),
new google.maps.LatLng(17.13560, 78.15470),
new google.maps.LatLng(17.13585, 78.15480),
new google.maps.LatLng(17.13600, 78.15600),
new google.maps.LatLng(17.13700, 78.15650),
new google.maps.LatLng(17.13720, 78.15750),
new google.maps.LatLng(17.13740, 78.15760),
new google.maps.LatLng(17.13742, 78.15780),
new google.maps.LatLng(17.13750, 78.15800),
new google.maps.LatLng(17.13770, 78.15805),
new google.maps.LatLng(17.13800, 78.15830),
new google.maps.LatLng(17.13810, 78.15835),
new google.maps.LatLng(17.13830, 78.15860),
new google.maps.LatLng(17.13850, 78.15870),
new google.maps.LatLng(17.13860, 78.15875),
new google.maps.LatLng(17.13900, 78.15845),
new google.maps.LatLng(17.13940, 78.15860),
new google.maps.LatLng(17.13960, 78.15875),
new google.maps.LatLng(17.13980, 78.15895),
new google.maps.LatLng(17.14000, 78.15897),
new google.maps.LatLng(17.14200, 78.16000),
];
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=&signed_in=true&libraries=visualization&callback=initMap">
</script>
</body>
</html>
[/c]