243 lines
7.6 KiB
PHP
243 lines
7.6 KiB
PHP
<?php
|
|
require '../backend.php';
|
|
error_reporting(E_ALL);
|
|
ini_set('display_errors',1);
|
|
//PostgreSQL
|
|
$gps_db_host = $savvyext->cfgReadChar('gpsdatabase.host');
|
|
$gps_db_port = $savvyext->cfgReadLong('gpsdatabase.port');
|
|
$gps_db_name = $savvyext->cfgReadChar('gpsdatabase.name');
|
|
$gps_db_user = $savvyext->cfgReadChar('gpsdatabase.user');
|
|
$gps_db_pass = $savvyext->cfgReadChar('gpsdatabase.pass');
|
|
$conn_string_gps = "host=${gps_db_host} port=${gps_db_port} dbname=${gps_db_name} user=${gps_db_user} password=${gps_db_pass}";
|
|
$pgconn_gps = pg_connect($conn_string_gps);
|
|
|
|
$res = [];
|
|
$min_lat = PHP_INT_MAX;
|
|
$min_lng = PHP_INT_MAX;
|
|
$max_lat = PHP_INT_MIN;
|
|
$max_lng = PHP_INT_MIN;
|
|
$center_lat = 0;
|
|
$center_lng = 0;
|
|
$member_id=$_GET['member_id']??0;
|
|
$q= "select traked_group, ttime::date as tracked_date, count(*) as point from members_tracking where member_id=".$member_id." group by traked_group,ttime::date ORDER by tracked_date DESC";
|
|
$q="
|
|
select traked_group, tracked_date, count(*) as point from
|
|
(select distinct traked_group,lat,lng, ttime::date as tracked_date from members_tracking where member_id=".$member_id."
|
|
group by traked_group,lat,lng,ttime::date ORDER by tracked_date DESC) as summary
|
|
group by summary.traked_group, summary.tracked_date
|
|
ORDER by summary.tracked_date DESC
|
|
LIMIT 50";
|
|
$r = pg_query($pgconn_gps, $q);
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>GPS speed test</title>
|
|
<style>
|
|
/* Always set the map height explicitly to define the size of the div
|
|
* element that contains the map. */
|
|
#map {
|
|
height: 625px;
|
|
}
|
|
/* Optional: Makes the sample page fill the window. */
|
|
html, body {
|
|
height: 100%;
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
.container {
|
|
float:left;
|
|
width:100%;
|
|
display: inline-block;
|
|
vertical-align: top; /* here */
|
|
}
|
|
.container .one{
|
|
width:30%;
|
|
float:left
|
|
}
|
|
.container .two{
|
|
width:67%;
|
|
float:left
|
|
}
|
|
/* .one{width:30%}
|
|
.two{width:70%} */
|
|
table, th, td {
|
|
border: 1px solid black;
|
|
border-collapse: collapse;
|
|
}
|
|
th, td {
|
|
padding: 5px;
|
|
}
|
|
</style>
|
|
<script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class = "one">
|
|
<table border="0" width="100%">
|
|
<tr>
|
|
<th>Group</th>
|
|
<th>Date</th>
|
|
<th>Point</th>
|
|
<th>View</th>
|
|
</tr>
|
|
<?php
|
|
while ($f=pg_fetch_assoc($r)) {
|
|
?>
|
|
|
|
|
|
<tr>
|
|
<td><?=$f["traked_group"]?></td>
|
|
<td><?=$f["tracked_date"]?></td>
|
|
<td><?=$f["point"]?></td>
|
|
<td><a href="?member_id=<?=$member_id?>&traked_group=<?=$f["traked_group"]?>&tracked_date=<?=$f["tracked_date"]?>">View => </a></td>
|
|
</tr>
|
|
<?php } ?>
|
|
</table>
|
|
</div>
|
|
<?php
|
|
|
|
$traked_group=$_GET['traked_group']??0;
|
|
$tracked_date=$_GET['tracked_date']??0;
|
|
if($member_id>0 && $traked_group>0){
|
|
$q = "select distinct a.traked_group, a.lat, a.lng, extract(epoch from a.ttime-b.ttime) as seconds,a.* from members_tracking a left join members_tracking b on (b.id=a.previous_id) ";
|
|
$q.= "where a.member_id=".$member_id." and a.traked_group='".$traked_group."' and a.ttime::date='".$tracked_date."' order by a.ttime asc";
|
|
$r = pg_query($pgconn_gps, $q);
|
|
while ($f=pg_fetch_assoc($r)) {
|
|
$res[] = $f;
|
|
if ($f["lat"]<$min_lat) $min_lat = $f["lat"];
|
|
if ($f["lng"]<$min_lng) $min_lng = $f["lng"];
|
|
if ($f["lat"]>$max_lat) $max_lat = $f["lat"];
|
|
if ($f["lng"]>$max_lng) $max_lng = $f["lng"];
|
|
}
|
|
$center_lat = ($min_lat+$max_lat)/2;
|
|
$center_lng = ($min_lng+$max_lng)/2;
|
|
pg_close($pgconn_gps);
|
|
}
|
|
?>
|
|
<div class = "two">
|
|
<div id="map">
|
|
</div>
|
|
<canvas id="line-chart" width="800" height="450"></canvas>
|
|
<br/>
|
|
<?= distanceBetweenTwoGpsCoordinates($min_lat,$min_lng,$max_lat,$max_lng,'K'); ?>
|
|
<table width="100%">
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Group</th>
|
|
<th>GPS</th>
|
|
<th>Duration</th>
|
|
<th>Seconds</th>
|
|
<th>Speed</th>
|
|
<th>Syn.speed</th>
|
|
<th>Distance</th>
|
|
<th>Time</th>
|
|
</tr>
|
|
<? foreach ($res as $f) { ?>
|
|
<tr>
|
|
<td><?=$f["id"]?></td>
|
|
<td><?=$f["traked_group"]?></td>
|
|
<td><?=$f["lat"]?>,<?=$f["lng"]?></td>
|
|
<td><?=$f["duration"]?></td>
|
|
<td><?=strtok($f["seconds"],'.')?></td>
|
|
<td><?=sprintf("%0.02f",$f["speed"])?></td>
|
|
<td><?=$f["seconds"]>0?sprintf("%0.02f",$f["distance"]/$f["seconds"]):0?></td>
|
|
<td><?=$f["distance"]?></td>
|
|
<td><?=strtok($f["ttime"],'.')?></td>
|
|
</tr>
|
|
<? } ?>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
var map;
|
|
function initMap() {
|
|
map = new google.maps.Map(document.getElementById('map'), {
|
|
zoom: 13,
|
|
center: {lat: <?=$center_lat?>, lng: <?=$center_lng?>},
|
|
mapTypeId: 'roadmap'
|
|
});
|
|
var flightPlanCoordinates = [
|
|
<? for ($i=0; $i<count($res)-1;$i++) {
|
|
echo " {lat: ".$res[$i]["lat"].", lng: ".$res[$i]["lng"]."},\n";
|
|
} echo " {lat: ".$res[count($res)-1]["lat"].", lng: ".$res[count($res)-1]["lng"]."}\n";?>
|
|
];
|
|
var flightPath = new google.maps.Polyline({
|
|
path: flightPlanCoordinates,
|
|
geodesic: true,
|
|
strokeColor: '#FF0000',
|
|
strokeOpacity: 1.0,
|
|
strokeWeight: 2
|
|
});
|
|
flightPath.setMap(map);
|
|
<? for ($i=0; $i<count($res);$i++) { ?>
|
|
var marker<?=$i?> = new google.maps.Marker({
|
|
position: {lat: <?=$res[$i]['lat']?>, lng: <?=$res[$i]['lng']?>},
|
|
map: map,
|
|
<? if ($res[$i]['id']>1961744 || $res[$i]['id']<1961502) { ?>
|
|
icon: {
|
|
url: "http://maps.google.com/mapfiles/ms/icons/blue-dot.png"
|
|
},
|
|
<? } ?>
|
|
title: '<?=$res[$i]["ttime"]?> - <?=$res[$i]["speed"]?>, coord: <?=$res[$i]["lat"]?>,<?=$res[$i]["lng"]?>'
|
|
});
|
|
marker<?=$i?>.addListener('click', function() {
|
|
//map.setZoom(8);
|
|
//map.setCenter(marker.getPosition());
|
|
alert(marker<?=$i?>.getTitle());
|
|
});
|
|
<? } ?>
|
|
}
|
|
</script>
|
|
<script>
|
|
$(function() {
|
|
let chart = new Chart(
|
|
document.getElementById("line-chart"), {
|
|
type: 'line',
|
|
data: {
|
|
labels: [''<? foreach ($res as $f) echo ",'".strtok($f["ttime"],'.')."'"; ?>],
|
|
datasets: [{
|
|
data: [0<? foreach ($res as $f) echo ",".$f["speed"]; ?>],
|
|
label: "Speed",
|
|
borderColor: "#3e95cd",
|
|
fill: false
|
|
}],
|
|
},
|
|
options: {
|
|
title: {
|
|
display: true,
|
|
text: 'Speed'
|
|
}
|
|
}
|
|
}
|
|
);
|
|
});
|
|
</script>
|
|
<script async defer
|
|
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDvjiRTxngOQyBP4zpqFlZuiquc0ROvo9c&libraries=visualization&callback=initMap">
|
|
</script>
|
|
</body>
|
|
</html>
|
|
<?
|
|
function distanceBetweenTwoGpsCoordinates($lat1,$lon1,$lat2,$lon2,$unit) {
|
|
//error_log("public function distanceBetweenTwoGpsCoordinates($lat1,$lon1,$lat2,$lon2,$unit)");
|
|
if (($lat1 == $lat2) && ($lon1 == $lon2)) {
|
|
return 0;
|
|
}
|
|
$theta = $lon1 - $lon2;
|
|
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
|
|
$dist = acos($dist);
|
|
$dist = rad2deg($dist);
|
|
$miles = $dist * 60 * 1.1515;
|
|
$unit = strtoupper($unit);
|
|
if ($unit == "K") {
|
|
return ($miles * 1.609344);
|
|
}
|
|
if ($unit == "N") {
|
|
($miles * 0.8684);
|
|
}
|
|
return $miles;
|
|
}
|