Show data on Google Maps » History » Version 1
Anonymous, 27/05/2015 13:21
1 | 1 | Anonymous | h1. Show data on Google Maps |
---|---|---|---|
2 | 1 | Anonymous | |
3 | 1 | Anonymous | The following HTML code uses the Google Maps API to display data as Heatmap + Marker. |
4 | 1 | Anonymous | Additional information are also provided when the cursor is put on the marker. |
5 | 1 | Anonymous | |
6 | 1 | Anonymous | <pre><code class="html"> |
7 | 1 | Anonymous | <!DOCTYPE html> |
8 | 1 | Anonymous | <html> |
9 | 1 | Anonymous | <head> |
10 | 1 | Anonymous | <meta charset="utf-8"> |
11 | 1 | Anonymous | <title>Heatmaps</title> |
12 | 1 | Anonymous | <style> |
13 | 1 | Anonymous | html, body, #map-canvas { |
14 | 1 | Anonymous | height: 100%; |
15 | 1 | Anonymous | margin: 0px; |
16 | 1 | Anonymous | padding: 0px |
17 | 1 | Anonymous | } |
18 | 1 | Anonymous | #panel { |
19 | 1 | Anonymous | position: absolute; |
20 | 1 | Anonymous | top: 5px; |
21 | 1 | Anonymous | left: 50%; |
22 | 1 | Anonymous | margin-left: -180px; |
23 | 1 | Anonymous | z-index: 5; |
24 | 1 | Anonymous | background-color: #fff; |
25 | 1 | Anonymous | padding: 5px; |
26 | 1 | Anonymous | border: 1px solid #999; |
27 | 1 | Anonymous | } |
28 | 1 | Anonymous | </style> |
29 | 1 | Anonymous | <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=visualization&sensor=true"></script> |
30 | 1 | Anonymous | <script> |
31 | 1 | Anonymous | |
32 | 1 | Anonymous | // This example creates a heatmap + marker on the map, over the IAS |
33 | 1 | Anonymous | var map, pointarray, heatmap; |
34 | 1 | Anonymous | |
35 | 1 | Anonymous | var citymap = {}; |
36 | 1 | Anonymous | |
37 | 1 | Anonymous | citymap['IAS'] = { |
38 | 1 | Anonymous | title: 'IAS', |
39 | 1 | Anonymous | center: new google.maps.LatLng(48.698972, 2.174751), |
40 | 1 | Anonymous | visits: 1, |
41 | 1 | Anonymous | city: 'Orsay', |
42 | 1 | Anonymous | country: 'France' |
43 | 1 | Anonymous | }; |
44 | 1 | Anonymous | |
45 | 1 | Anonymous | // Create the heatmap gradient with five points |
46 | 1 | Anonymous | var ipData = [ |
47 | 1 | Anonymous | new google.maps.LatLng(48.698972, 2.174751), |
48 | 1 | Anonymous | new google.maps.LatLng(48.698972, 2.174751), |
49 | 1 | Anonymous | new google.maps.LatLng(48.698972, 2.174751), |
50 | 1 | Anonymous | new google.maps.LatLng(48.698972, 2.174751), |
51 | 1 | Anonymous | new google.maps.LatLng(48.698972, 2.174751), |
52 | 1 | Anonymous | ]; |
53 | 1 | Anonymous | |
54 | 1 | Anonymous | function initialize() { |
55 | 1 | Anonymous | |
56 | 1 | Anonymous | // Centering and type of shown map |
57 | 1 | Anonymous | var mapOptions = { |
58 | 1 | Anonymous | zoom: 7, |
59 | 1 | Anonymous | center: new google.maps.LatLng(48.698972, 2.174751), |
60 | 1 | Anonymous | mapTypeId: google.maps.MapTypeId.SATELLITE //TERRAIN |
61 | 1 | Anonymous | }; |
62 | 1 | Anonymous | |
63 | 1 | Anonymous | map = new google.maps.Map(document.getElementById('map-canvas'), |
64 | 1 | Anonymous | mapOptions); |
65 | 1 | Anonymous | |
66 | 1 | Anonymous | // Construct the marker for each value in citymap (just one, in this case). |
67 | 1 | Anonymous | for (var city in citymap) { |
68 | 1 | Anonymous | var contentString = citymap[city].title+' ('+citymap[city].city+')'+': '+citymap[city].visits; |
69 | 1 | Anonymous | var populationOptions = { |
70 | 1 | Anonymous | map: map, |
71 | 1 | Anonymous | position: citymap[city].center, |
72 | 1 | Anonymous | title: contentString, |
73 | 1 | Anonymous | icon: 'http://szcluster-db.ias.u-psud.fr/sitools/upload/locMarker_redCircle.gif', |
74 | 1 | Anonymous | }; |
75 | 1 | Anonymous | |
76 | 1 | Anonymous | // Add the circle for this city to the map. |
77 | 1 | Anonymous | cityCircle = new google.maps.Marker(populationOptions); //Marker |
78 | 1 | Anonymous | |
79 | 1 | Anonymous | } |
80 | 1 | Anonymous | |
81 | 1 | Anonymous | var pointArray = new google.maps.MVCArray(ipData); |
82 | 1 | Anonymous | |
83 | 1 | Anonymous | heatmap = new google.maps.visualization.HeatmapLayer({ |
84 | 1 | Anonymous | data: pointArray, |
85 | 1 | Anonymous | dissipating: 1, |
86 | 1 | Anonymous | opacity: 1, |
87 | 1 | Anonymous | maxIntensity:5, |
88 | 1 | Anonymous | radius: 15 |
89 | 1 | Anonymous | }); |
90 | 1 | Anonymous | |
91 | 1 | Anonymous | heatmap.setMap(map); |
92 | 1 | Anonymous | } |
93 | 1 | Anonymous | |
94 | 1 | Anonymous | function toggleHeatmap() { |
95 | 1 | Anonymous | heatmap.setMap(heatmap.getMap() ? null : map); |
96 | 1 | Anonymous | } |
97 | 1 | Anonymous | |
98 | 1 | Anonymous | function changeGradient() { |
99 | 1 | Anonymous | var gradient = [ |
100 | 1 | Anonymous | 'rgba(0, 255, 255, 0)', |
101 | 1 | Anonymous | 'rgba(0, 255, 255, 1)', |
102 | 1 | Anonymous | 'rgba(0, 191, 255, 1)', |
103 | 1 | Anonymous | 'rgba(0, 127, 255, 1)', |
104 | 1 | Anonymous | // 'rgba(0, 63, 255, 1)', |
105 | 1 | Anonymous | // 'rgba(0, 0, 255, 1)', |
106 | 1 | Anonymous | // 'rgba(0, 0, 223, 1)', |
107 | 1 | Anonymous | // 'rgba(0, 0, 191, 1)', |
108 | 1 | Anonymous | // 'rgba(0, 0, 159, 1)', |
109 | 1 | Anonymous | // 'rgba(0, 0, 127, 1)', |
110 | 1 | Anonymous | // 'rgba(63, 0, 91, 1)', |
111 | 1 | Anonymous | // 'rgba(127, 0, 63, 1)', |
112 | 1 | Anonymous | 'rgba(191, 0, 31, 1)', |
113 | 1 | Anonymous | // 'rgba(255, 0, 0, 1)' |
114 | 1 | Anonymous | ] |
115 | 1 | Anonymous | heatmap.set('gradient', heatmap.get('gradient') ? null : gradient); |
116 | 1 | Anonymous | } |
117 | 1 | Anonymous | |
118 | 1 | Anonymous | function changeRadius() { |
119 | 1 | Anonymous | heatmap.set('radius', heatmap.get('radius') ? null : 10); |
120 | 1 | Anonymous | } |
121 | 1 | Anonymous | |
122 | 1 | Anonymous | function changeOpacity() { |
123 | 1 | Anonymous | heatmap.set('opacity', heatmap.get('opacity') ? 0.8 : 1); |
124 | 1 | Anonymous | } |
125 | 1 | Anonymous | |
126 | 1 | Anonymous | google.maps.event.addDomListener(window, 'load', initialize); |
127 | 1 | Anonymous | |
128 | 1 | Anonymous | </script> |
129 | 1 | Anonymous | </head> |
130 | 1 | Anonymous | |
131 | 1 | Anonymous | <body> |
132 | 1 | Anonymous | <div id="panel"> |
133 | 1 | Anonymous | <button onclick="toggleHeatmap()">Toggle Heatmap</button> |
134 | 1 | Anonymous | <button onclick="changeGradient()">Change gradient</button> |
135 | 1 | Anonymous | <button onclick="changeRadius()">Change radius</button> |
136 | 1 | Anonymous | <button onclick="changeOpacity()">Change opacity</button> |
137 | 1 | Anonymous | </div> |
138 | 1 | Anonymous | <div id="map-canvas"></div> |
139 | 1 | Anonymous | </body> |
140 | 1 | Anonymous | </html> |
141 | 1 | Anonymous | </code></pre> |
142 | 1 | Anonymous | |
143 | 1 | Anonymous | The browser will display the HTML in this way: |
144 | 1 | Anonymous | |
145 | 1 | Anonymous | !dataOnMaps.png! |