Introduction
This is a sample on how to create a Google Heatmap.
Background
A few weeks ago, I had to create a Goggle Heatmap showing where our customers resided. I found a good sample on how to get coordinates:
However, I found no sample on how to create the Heatmap. So I wrote this.
Using the Code
Start by creating an empty web in Visual Studio. Add the pages from below. After that, start the web and click GetData. Watch the results. Click again. The
Heatmap then gets new data.
The ASP Page: Default.aspx
<%@ Page Language="VB"
AutoEventWireup="true" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<html>
<head id="Head1" runat="server">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Patient locations</title>
<style type="text/css">
.formatText
{
color: Green;
font-size: 11px;
font-family: Arial;
font-weight: bold;
}
html
{
height: 100%;
}
body
{
height: 100%;
margin: 0px;
padding: 0px;
}
#map_canvas
{
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?
v=3.exp&sensor=false&libraries=visualization"></script>
<script type="text/javascript">
var map, pointarray, heatmap, patdata;
var gradient3 = [
'rgba(255,255,255,0)',
'rgba(0,43,255,1)',
'rgba(0,86,255,1)',
'rgba(0,127,255,1)',
'rgba(0,171,255,1)',
'rgba(0,213,255,1)',
'rgba(0,255,127,1)',
'rgba(0,255,255,1)',
'rgba(0,255,0,1)',
'rgba(127,255,0,1)',
'rgba(255,255,0,1)',
'rgba(255,213,0,1)',
'rgba(255,171,0,1)',
'rgba(255,127,0,1)',
'rgba(255,86,0,1)',
'rgba(255,43,0,1)',
'rgba(255,0,0,1)'
]
function initialize() {
var mapOptions = {
zoom: 10, center: new google.maps.LatLng(59.3289, 18.06491), mapTypeId: google.maps.MapTypeId.ROADMAP };
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
if (typeof locationList != 'undefined') {
patdata = [];
for (var i = 0; i < locationList.length; i++) {
var args = locationList[i].split(",");
patdata.push(new google.maps.LatLng(args[0], args[1]));
}
pointArray = new google.maps.MVCArray(patdata);
heatmap = new google.maps.visualization.HeatmapLayer({ data: pointArray });
heatmap.setOptions({
gradient: heatmap.get('gradient') ? null : gradient3,
maxIntensity: 10, opacity: 0.9, radius: 20, dissipating: true });
heatmap.setMap(map);
}
locationList = [];
}
</script>
<script type="text/javascript">
var map;
function reload() {
var myLatlng = new google.maps.LatLng(59.3289, 18.06491);
var myOptions = {
zoom: 10,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
}
</script>
</head>
<body style="margin: 0px; padding: 0px;" onload="initialize()">
<form id="Form1" runat="server">
<asp:button id="btnFetch"
text="Get Map" runat="server">
<input id="Submit1" type="submit"
value="Reset" onclick="reload()" />
<div style="padding: 0%;">
<div id="map_canvas" style="width: 100%; height: 100%;">
</div>
</div>
</form>
</body>
</html>
The code-behind in VB: Default.aspx.vb
Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnFetch_Click(sender As Object, e As EventArgs) Handles btnFetch.Click
Dim toggle As Boolean
If (Me.ViewState("toggle") IsNot Nothing) Then
toggle = CType(Me.ViewState("toggle"), Boolean)
Else
toggle = False
End If
Dim oGeocodeList As List(Of String)
If Not toggle Then
oGeocodeList = New List(Of String)() From { _
" '59.4847444, 17.749211'", _
" '59.4209403, 17.797933 '", _
" '59.5150872, 17.6437817 '" _
}
Me.ViewState.Add("toggle", True)
Else
oGeocodeList = New List(Of String)() From {" '59.3289, 18.06491'"}
Me.ViewState.Add("toggle", False)
End If
Dim geocodevalues = String.Join(",", oGeocodeList.ToArray())
ClientScript.RegisterArrayDeclaration("locationList", geocodevalues)
End Sub
End Class