index.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html >
  2. <head>
  3. <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5. <title>Arduino/PHP/MySQL & Google Maps</title>
  6. <script language="javascript" type="text/javascript" src="../flot/jquery.js"></script>
  7. <script src="https://maps.googleapis.com/maps/api/js?key=<KEY>"
  8. type="text/javascript"></script>
  9. <script type="text/javascript">
  10. //<![CDATA[
  11. $(window).resize(function() {
  12. $('#map').height($(window).height());
  13. $('#map').width($(window).width());
  14. });
  15. function load() {
  16. $('#map').height($(window).height());
  17. $('#map').width($(window).width());
  18. var map = new google.maps.Map(document.getElementById("map"), {
  19. center: new google.maps.LatLng(51.44, 11.0),
  20. zoom: 13,
  21. mapTypeId: 'terrain'
  22. });
  23. var bikeLayer = new google.maps.BicyclingLayer();
  24. bikeLayer.setMap(map);
  25. var infoWindow = new google.maps.InfoWindow;
  26. $.ajax({
  27. type: 'POST',
  28. url: 'gps_ajax.php',
  29. data: {},
  30. success: function(txt) {
  31. var data = JSON.parse(txt);
  32. var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  33. var labelIndex = 0, startIndex = 0, distances = [0], timeDiff = [1], speed = [0];
  34. for (var i = 1; i < data.length; i++) {
  35. distances[i] = distance(data[i-1].lat, data[i-1].lng, data[i].lat, data[i].lng);
  36. if(isNaN(distances[i])) distances[i] = 0;
  37. timeDiff[i] = data[i].timestamp - data[i-1].timestamp;
  38. speed[i] = distances[i] / timeDiff[i] * 3.6;
  39. if(timeDiff[i] > 60 || i == data.length - 1) {
  40. var label = labels[labelIndex++ % labels.length];
  41. var html = "Start: <b>" + data[startIndex].format + "</b><br/>" + data[startIndex].alt + "m<br/>Finish: <b>" + data[i-1].format + "</b><br/>" + data[i-1].alt + "m";
  42. var startMarker = new google.maps.Marker({
  43. map: map,
  44. position: data[startIndex],
  45. label: label
  46. });
  47. bindInfoWindow(startMarker, map, infoWindow, html);
  48. var color = getRandomColor();
  49. var path = new google.maps.Polyline({
  50. map: map,
  51. path: data.slice(startIndex, i),
  52. strokeColor: color,
  53. strokeOpacity: 1,
  54. strokeWeight: 4,
  55. zIndex: 1
  56. });
  57. google.maps.event.addListener(path, 'mouseover', function(event) {
  58. this.setOptions({
  59. zIndex: 2,
  60. strokeWeight: 6
  61. });
  62. });
  63. google.maps.event.addListener(path, 'mouseout', function(event) {
  64. this.setOptions({
  65. zIndex: 1,
  66. strokeWeight: 4
  67. });
  68. });
  69. var dis = distances.slice(startIndex, i).reduce((a, b) => a + b, 0).toFixed(1);
  70. var tsp = Math.max.apply(Math, speed.slice(startIndex, i)).toFixed(2);
  71. var avg = (dis / (data[i-1].timestamp - data[startIndex].timestamp) * 3.6).toFixed(2);
  72. $("#info").html($("#info").html()+"<div style='border-radius: 25px;opacity:.7;padding:5px;margin:5px;display: inline-block;background-color:"+color+"'>"+html+"<br/>Distance: "+dis+"m<br/>top speed: "+tsp+"km/h<br/>average speed: "+avg+"km/h</div>");
  73. startIndex = i;
  74. }
  75. var circle = new google.maps.Circle({
  76. strokeColor: '#FF0000',
  77. strokeOpacity: 0.0,
  78. strokeWeight: 0,
  79. fillColor: '#FF0000',
  80. fillOpacity: 0.01,
  81. map: map,
  82. center: data[i],
  83. radius: data[i].hdop / 100,
  84. zIndex: 0
  85. });
  86. }
  87. }
  88. });
  89. }
  90. function bindInfoWindow(marker, map, infoWindow, html) {
  91. google.maps.event.addListener(marker, 'click', function() {
  92. infoWindow.setContent(html);
  93. infoWindow.open(map, marker);
  94. });
  95. }
  96. function getRandomColor() {
  97. var letters = '3456789ABCDEF'.split('');
  98. var color = '#';
  99. for (var i = 0; i < 6; i++ ) {
  100. color += letters[Math.floor(Math.random() * 13)];
  101. }
  102. return color;
  103. }
  104. /*function convertTimestamp(sqlTs) {
  105. var t = sqlTs.split(/[- :]/);
  106. return {
  107. ts: new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]),
  108. format: t[2]+"."+t[1]+"."+t[0]+" "+t[3]+":"+t[4]+":"+t[5]
  109. };
  110. }*/
  111. function distance (lat1, lon1, lat2, lon2) {
  112. // Convert degrees to radians
  113. lat1 = lat1 * Math.PI / 180.0;
  114. lon1 = lon1 * Math.PI / 180.0;
  115. lat2 = lat2 * Math.PI / 180.0;
  116. lon2 = lon2 * Math.PI / 180.0;
  117. // radius of earth in metres
  118. var r = 6378100;
  119. // P
  120. var rho1 = r * Math.cos(lat1);
  121. var z1 = r * Math.sin(lat1);
  122. var x1 = rho1 * Math.cos(lon1);
  123. var y1 = rho1 * Math.sin(lon1);
  124. // Q
  125. var rho2 = r * Math.cos(lat2);
  126. var z2 = r * Math.sin(lat2);
  127. var x2 = rho2 * Math.cos(lon2);
  128. var y2 = rho2 * Math.sin(lon2);
  129. // Dot product
  130. var dot = (x1 * x2 + y1 * y2 + z1 * z2);
  131. var cos_theta = dot / (r * r);
  132. var theta = Math.acos(cos_theta);
  133. // Distance in Metres
  134. return r * theta;
  135. }
  136. //]]>
  137. </script>
  138. </head>
  139. <body onload="load()" style="margin: 0; padding: 0;">
  140. <div id="map" style="width: 800px; height: 800px"></div>
  141. <div id="info" style="position:absolute; bottom:0; left:0;"></div>
  142. </body>
  143. </html>