Last active 1440821788

Google Maps display for a csv-formatted list of dates/latitudes/longitudes/accuracy-in-metres.

Revision 3a1e669d0b615d0edba5d5aa3d8a4c3f2aeb5603

gpstrack.php Raw
1<?php
2
3$passwdfile = "/path/to/file";
4$logpath = "/path/to/file"; // should be a csv file with date,lat,long,hor-acc
5
6// For json
7foreach (apache_request_headers() as $header => $value) { if ($header == "Accept") { $accept = $value; } }
8
9// Variable checks
10$allowed = isset($_GET['allowed']);
11if ($allowed && !$_GET['allowed'] == file_get_contents($passwdfile)) {
12 die("Sorry, this needs a password now.<form><input type=text name=allowed><input type=submit></form>");
13}
14$nocir = !isset($_GET['nocircles']);
15if (isset($_GET['count'])) {
16 $count = intval($_GET['count']);
17} else {
18 $count = 10;
19}
20
21// Getting data
22if (isset($_GET['date']) && intval($_GET['date']) != 0 && strlen($_GET['date']) == 8) {
23 $date = intval($_GET['date']);
24 $datarr = explode("\n", shell_exec("grep $date $logpath --color=never"));
25 array_pop($datarr);
26} else {
27 $datarr = explode("\n", shell_exec("tail -n$count $logpath"));
28}
29
30// Correcting for allowed data
31$last = str_getcsv(array_pop($datarr));
32if (!$allowed) $last = array($last[0], substr($last[1],0,6), substr($last[2],0,6));
33
34// Creating static image url
35$imageurl = "https://maps.googleapis.com/maps/api/staticmap?scale=2&format=jpg&zoom=";
36if ($allowed) $imageurl .= "18";
37else $imageurl .= "15";
38$imageurl .= "&center=" . $last[1] . "," . $last[2];
39if ($allowed) $imageurl .= "&markers=" . $last[1] . "," . $last[2];
40
41// Json returning
42if (isset($_GET['json'])) {
43 header("Content-Type: application/json");
44 $arr = array();
45 foreach ($datarr as $item) {
46 $tmp = str_getcsv($item);
47 if (!$allowed) $tmp = array($tmp[0], substr($tmp[1],0,6), substr($tmp[2],0,6), "1111");
48 $arr[] = $tmp;
49 }
50 echo json_encode(array("markers" => $arr, "imageurl" => $imageurl));
51 die();
52}
53
54// Mobile support {
55require_once 'Mobile_Detect.php'; // http://mobiledetect.net/ remove this block if you don't need this functionality
56$detect = new Mobile_Detect;
57if (isset($_GET['mobile']) || $detect->isMobile()) { ?>
58 <head>
59 <meta http-equiv="refresh" content="60">
60 <script>
61 document.addEventListener('DOMContentLoaded',function(){
62 var w = Math.floor(Math.max(document.documentElement.clientWidth, window.innerWidth || 0) / 2)
63 var h = Math.floor(Math.max(document.documentElement.clientHeight, window.innerHeight || 0) / 2)
64 document.getElementById('i').src = "<?php echo $imageurl; ?>&size=" + w + "x" + h;
65 });
66 </script>
67 </head>
68 <body>
69 <img src="nothing" id="i" />
70 </body>
71<?php die();}
72// }
73
74// Generating lists of markers/circles
75if ($allowed) {
76 $markers = "[";
77 if ($nocir) {
78 $circles = "[";
79 $ciroptions = "{strokeColor:'#FF0000',strokeOpacity:0.8,strokeWeight:2,fillColor:'#AA0000',fillOpacity:0.2,map:map";
80 }
81 foreach ($datarr as $item) {
82 $item = str_getcsv($item);
83 $markers .= 'new google.maps.Marker({map: map, position: new google.maps.LatLng(' . $item[1] . ', ' . $item[2] . '),title: "' . $item[0] . ' to ' . $item[3] . 'm",icon: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"}),';
84 if ($nocir) {
85 $circles .= 'new google.maps.Circle(' . $ciroptions . ',radius: ' . $item[3] . '}),';
86 }
87 }
88 $markers .= ']';
89 if ($nocir) {
90 $circles .= ']';
91 }
92}
93
94// Start html
95?>
96<!DOCTYPE html>
97<html>
98 <head>
99<?php if ($count < 50) { ?>
100 <meta http-equiv="refresh" content="60">
101<?php } ?>
102 <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
103 <style type="text/css">
104 html { height: 100% }
105 body { height: 100%; margin: 0; padding: 0 }
106 #map-canvas { height: 100% }
107 </style>
108 <script type="text/javascript"
109 src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCE7QCD1wjjjspuEa7ABtKGpJxttNOgtIU&sensor=false">
110 </script>
111 <script type="text/javascript">
112 var geocoder;
113 var map;
114 var markers;
115 var circles;
116 var topcircle;
117<?php if (!$allowed) { ?>
118 var data = <?php echo json_encode($last); ?>;
119 var coords = [new google.maps.LatLng(data[1], data[2])];
120<?php } ?>
121 function initialize() {
122 geocoder = new google.maps.Geocoder();
123 var mapOptions = {
124 center: <?php echo 'new google.maps.LatLng(' . $last[1] . ', ' . $last[2] . ')'; ?>,
125 zoom: <?php if (isset($_GET['zoom'])) { echo intval($_GET['zoom']); } else { if ($allowed) { echo "18"; } else { echo "15"; } }; echo "\n"; ?>
126 };
127 map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
128<?php if ($allowed) { ?>
129 markers = <?php echo $markers; ?>;
130 markers[markers.length-1].setOptions({animation:google.maps.Animation.DROP,icon:"http://maps.google.com/mapfiles/ms/icons/blue-dot.png",zIndex:100});
131 var coords = [];
132 for (i=0;i<markers.length;i++) {
133 coords.push(markers[i].getPosition());
134 }
135 var recentPath = new google.maps.Polyline({
136 path: coords,
137 geodesic: true,
138 strokeColor: '#FF0000',
139 strokeOpacity: 1.0,
140 strokeWeight: 2
141 });
142 recentPath.setMap(map);
143<?php if ($nocir) { ?>
144 circles = <?php echo $circles; ?>;
145 for (i=0;i<circles.length;i++) {
146 circles[i].bindTo('center', markers[i], 'position');
147 markers[i]._myCircle = circles[i];
148 circles[i].title = markers[i].title;
149 }
150 topcircle = circles[circles.length-1];
151 topcircle.setOptions({fillColor: '#0000AA', strokeColor: '#0000FF', url: "<?php echo $_SERVER['REQUEST_URI']."&nocircles" ?>"});
152 google.maps.event.addListener(topcircle, 'click', function() {
153 window.location = topcircle.url;
154 });
155<?php } ?>
156<?php } else { ?>
157 var infowindow = new google.maps.InfoWindow({
158 content: "blha303's last recorded location (accurate to 1.1km approx)"
159 });
160 var circle = new google.maps.Circle({
161 map: map,
162 center: coords[coords.length-1],
163 radius: 1100,
164 fillColor: '#AA0000',
165 fillOpacity: 0.1
166 });
167 google.maps.event.addListener(circle, 'click', function() {
168 infowindow.open(map,circle);
169 });
170<?php } ?>
171 }
172 google.maps.event.addDomListener(window, 'load', initialize);
173 </script>
174 </head>
175 <body>
176 <div id="map-canvas"/>
177 </body>
178</html>