<?php /**/ ?><?php
// http://earth.google.com/kml/kml_tut.html#tracking_point
// get the time
$timesnap = date("H:i:s");

// split the client's BBOX return by commas and spaces to obtain an array of coordinates
$coords = preg_split('/,|\s/', $_GET["BBOX"]);

// for clarity, place each coordinate into a clearly marked bottom_left or top_right variable
$bl_lon = $coords[0];
$bl_lat = $coords[1];
$tr_lon = $coords[2];
$tr_lat = $coords[3];

// calculate the approx center of the view -- note that this is innaccurate if the user is not looking straight down
$userlon = (($coords[2] - $coords[0])/2) + $coords[0];
$userlat = (($coords[3] - $coords[1])/2) + $coords[1];

$response = '<?xml version="1.0" encoding="UTF-8"?>';
$response .= '<kml xmlns="http://earth.google.com/kml/2.0">';
$response .= '<Placemark>';
$response .= '<name>'.$timesnap.'</name>';
$response .= '<Point>';
$response .= "<coordinates>$userlon,$userlat,0</coordinates>";
$response .= '</Point>';
$response .= '</Placemark>';
$response .= '</kml>';
# set $myKMLCode together as a string
 $downloadfile="myKml.kml"; # give a name to appear at the client
 header("Content-disposition: attachment; filename=$downloadfile");
 header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf8");
 header("Content-Transfer-Encoding: binary");
 header("Content-Length: ".strlen($response));
 header("Pragma: no-cache");
 header("Expires: 0");
echo $response;
?>
