-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGoogleMaps.html
94 lines (82 loc) · 2.41 KB
/
GoogleMaps.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
/* 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;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=[Your Google Maps JavaScript API Key]"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
var map;
var x;
var y;
var latlng;
function loadMap() {
$.getJSON("https://api.thingspeak.com/channels/920137/fields/4/last.json?api_key=[ThingSpeak Read API Key]", function (result) {
var m = result;
x = Number(m.field4);
});
$.getJSON("https://api.thingspeak.com/channels/920137/fields/5/last.json?api_key=[ThingSpeak Read API Key]", function (result) {
var m = result;
y = Number(m.field5);
}).done(function () {
initMap();
});
}
window.setInterval(function () { loadMap(); }, 15000);
function initMap() {
latlng = { lat: x, lng: y }
var mapOptions = {
zoom: 18,
center: latlng
}
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var geocoder = new google.maps.Geocoder;
var infowindow = new google.maps.InfoWindow;
geocodeLatLng(geocoder, map, infowindow)
}
function geocodeLatLng(geocoder, map, infowindow) {
//var input = document.getElementById('latlng').value;
//var latlngStr = input.split(',', 2);
//var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
geocoder.geocode({ 'location': latlng }, function (results, status) {
if (status === 'OK') {
if (results[0]) {
//map.setZoom(11);
var marker = new google.maps.Marker({
position: latlng,
map: map
});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
google.maps.event.addDomListener(window, 'load', initMap);
});
}
</script>
</head>
<body onload='loadMap()'>
<div id="map"></div>
</body>
</html>