startElement("Style");
$xml->writeAttribute("id", $name."Style");
$xml->startElement("IconStyle");
$xml->writeAttribute("id", $name."Icon");
$xml->startElement("Icon");
$xml->writeElement("href", $url);
$xml->endElement();
$xml->endElement();
$xml->endElement();
}
function toHMS($s) {
$d = floor($s/86400);
$h = floor(($s%86400)/3600);
$m = floor((($s%86400)%3600)/60);
$s = (($s%86400)%3600)%60;
return (($d>0)?($d." d "):"").(substr("00".$h,-2)).":".(substr("00".$m,-2)).":".(substr("00".$s,-2));
}
if ($trackid>0 && $userid>0) {
$query = $mysqli->prepare("SELECT positions.ID,Latitude,Longitude,Altitude,Speed,Angle,DateOccurred,username,Name FROM positions LEFT JOIN users ON (positions.FK_Users_ID=users.ID) LEFT JOIN trips ON (positions.FK_Trips_ID=trips.ID) WHERE positions.FK_Users_ID=? AND positions.FK_Trips_ID=? ORDER BY positions.DateOccurred");
$query->bind_param("ii", $userid, $trackid);
$query->execute();
$query->store_result();
$query->bind_result($positionid,$latitude,$longitude,$altitude,$speed,$angle,$dateoccured,$username,$trackname);
$query->fetch(); // take just one row to get trackname etc
$query->data_seek(0); // and reset result set
switch ($type) {
case "kml":
default:
header("Content-type: application/vnd.google-earth.kml+xml");
header("Content-Disposition: attachment; filename=\"track$trackid.kml\"");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->startElement("kml");
$xml->writeAttribute("xmlns", "http://earth.google.com/kml/2.1");
$xml->setIndent(true);
$xml->startElement("Document");
$xml->writeElement("name", $trackname);
// line style
$xml->startElement("Style");
$xml->writeAttribute("id", "lineStyle");
$xml->startElement("LineStyle");
$xml->writeElement("color","7f0000ff");
$xml->writeElement("width","4");
$xml->endElement();
$xml->endElement();
// marker styles
addStyle($xml,"red","http://maps.google.com/mapfiles/markerA.png");
addStyle($xml,"green","http://maps.google.com/mapfiles/marker_greenB.png");
addStyle($xml,"gray","http://labs.google.com/ridefinder/images/mm_20_gray.png");
$style = "#redStyle"; // for first element
$i = 0;
$totalMeters = 0;
$totalSeconds = 0;
while ($query->fetch()) {
$distance = (isset($prev_latitude))?haversine_distance($prev_latitude,$prev_longitude,$latitude,$longitude):0;
$prev_latitude = $latitude;
$prev_longitude = $longitude;
$seconds = (isset($prev_dateoccured))?(strtotime($dateoccured)-strtotime($prev_dateoccured)):0;
$prev_dateoccured = $dateoccured;
$totalMeters += $distance;
$totalSeconds += $seconds;
if(++$i == $query->num_rows) { $style = "#greenStyle"; } // last element
$xml->startElement("Placemark");
$xml->writeAttribute("id", $positionid);
//$xml->writeElement("name", $i);
$description =
"
".$lang_user.": ".strtoupper($username)."
".$lang_track.": ".strtoupper($trackname).
"
".
"".
"
".$lang_time.": ".$dateoccured."
".
(($speed)?"".$lang_speed.": ".round($speed*3.6,2*$factor_kmh)." ".$unit_kmh."
":"").
(($altitude != null)?"".$lang_altitude.": ".round($altitude*$factor_m)." ".$unit_m."
":"").
"".$lang_ttime.": ".toHMS($totalSeconds)."
".
"".$lang_aspeed.": ".(($totalSeconds!=0)?round($totalMeters/$totalSeconds*3.6*$factor_kmh,2):0)." ".$unit_kmh."
".
"".$lang_tdistance.": ".round($totalMeters/1000*$factor_km,2)." ".$unit_km."
"."
".
"
".$lang_point." ".$i." ".$lang_of." ".($query->num_rows-1)."
".
"
";
$xml->startElement("description");
$xml->writeCData($description);
$xml->endElement();
$xml->writeElement("styleUrl", $style);
$xml->startElement("Point");
$coordinate[$i] = $longitude.",".$latitude.(($altitude) ? ",".$altitude : "");
$xml->writeElement("coordinates", $coordinate[$i]);
$xml->endElement();
$xml->endElement();
$style = "#grayStyle"; // other elements
}
$coordinates = implode("\n",$coordinate);
$xml->startElement("Placemark");
$xml->writeElement("styleUrl", "#lineStyle");
$xml->startElement("LineString");
$xml->writeElement("coordinates", $coordinates);
$xml->endElement();
$xml->endElement();
$xml->endElement();
$xml->endElement();
$xml->endDocument();
$xml->flush();
break;
case "gpx":
header("Content-type: application/application/gpx+xm");
header("Content-Disposition: attachment; filename=\"track$trackid.gpx\"");
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument("1.0");
$xml->startElement("gpx");
$xml->writeAttribute("xmlns", "http://www.topografix.com/GPX/1/1");
$xml->writeAttribute("xmlns:gpxdata", "http://www.cluetrust.com/XML/GPXDATA/1/0");
$xml->writeAttribute("creator", "phpTrackme");
$xml->writeAttribute("version", "1.1");
$xml->startElement("metadata");
$xml->writeElement("name", $trackname);
$xml->writeElement("time", str_replace(" ","T",$dateoccured));
$xml->endElement();
$xml->startElement("trk");
$xml->writeElement("name", $trackname);
$xml->startElement("trkseg");
$i = 0;
$totalMeters = 0;
$totalSeconds = 0;
while ($query->fetch()) {
$distance = (isset($prev_latitude))?haversine_distance($prev_latitude,$prev_longitude,$latitude,$longitude):0;
$prev_latitude = $latitude;
$prev_longitude = $longitude;
$seconds = (isset($prev_dateoccured))?(strtotime($dateoccured)-strtotime($prev_dateoccured)):0;
$prev_dateoccured = $dateoccured;
$totalMeters += $distance;
$totalSeconds += $seconds;
$xml->startElement("trkpt");
$xml->writeAttribute("lat", $latitude);
$xml->writeAttribute("lon", $longitude);
if($altitude) { $xml->writeElement("ele", $altitude); }
$xml->writeElement("time", str_replace(" ","T",$dateoccured));
$xml->writeElement("name", ++$i);
$xml->startElement("desc");
$description =
$lang_user.": ".strtoupper($username)." ".$lang_track.": ".strtoupper($trackname).
" ".$lang_time.": ".$dateoccured.
(($speed)?" ".$lang_speed.": ".round($speed*3.6,2*$factor_kmh)." ".$unit_kmh:"").
(($altitude != null)?" ".$lang_altitude.": ".round($altitude*$factor_m)." ".$unit_m:"").
" ".$lang_ttime.": ".toHMS($totalSeconds)."".
" ".$lang_aspeed.": ".(($totalSeconds!=0)?round($totalMeters/$totalSeconds*3.6*$factor_kmh,2):0)." ".$unit_kmh.
" ".$lang_tdistance.": ".round($totalMeters/1000*$factor_km,2)." ".$unit_km.
" ".$lang_point." ".$i." ".$lang_of." ".($query->num_rows-1);
$xml->writeCData($description);
$xml->endElement();
$xml->endElement();
}
$xml->endElement();
$xml->endElement();
$xml->endElement();
$xml->endDocument();
$xml->flush();
break;
}
$query->free_result();
$query->close();
}
$mysqli->close();
?>