Trails Munich Horizon

Trails Munich Horizon

This is part 3 of the DIY Location mapping. This time we will integrate with google earth to see live updates from our devices, we tracked with part 1.

Therefore I have had the tracking running for some days with an update interval of 15 minutes, so that I does not drain my battery too much. By enhancing the update frequency you will get fine grained location data.

We need to add the following PHP script to deliver a valid KML file, which is loaded by another KML file. The second KML file is loaded into Google Earth and does poll data via the PHP scripts to enable the live updates.

This needs to be running on the linux web server, where we collected the data from part 1 and 2.

Trails Munich Helicopterview

Trails Munich Helicopterview

<?php
$coords = array();

$db = mysql_connect('MYSQL_HOST', 'MYSQL_USER', 'MYSQL_PASS');
mysql_select_db('MYSQL_DBNAME', $db) or die(mysql_error());

$query = "SELECT * FROM (SELECT * FROM history WHERE user = 'YOUR_USERNAME' AND dt > '2011-09-01 00:00:00' AND dt < '2011-11-01 00:00:00' ORDER BY dt  DESC LIMIT 500) AS tbl ORDER BY tbl.dt";
$result = mysql_query($query, $db) or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                                               // in ein Array $data gelesen
    $remap = array(
    'zeit' => $row['dt'],
    'lat' => $row['lat'],
    'lon' => $row['lng']
    );

  $coords[$row['user']][] = $remap;
}

mysql_free_result($result);

mysql_close($db);

$lat = '';
$lon = '';
$alt = '';
$kml ='
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
';
$kml .='    <Placemark>
      <name>Your Trail</name>
      <description>Your trail as it is recorded from his iPhone app</description>
      <styleUrl>#greenLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates>
';
if (Count($coords) > 0 ) {
  foreach ( $coords['mat'] AS $time => $coord) {
    $zeit = $coord['zeit'];
    $lat = $coord['lat'];
    $lon = $coord['lon'];
    $alt = "995.0";
    $kml .= $coord['lon'].",";
    $kml .= $coord['lat'].",";
    $kml .= "995.0,";
  }
  $kml = trim($kml);
}
$kml .='
        </coordinates>
      </LineString>
    </Placemark>
';

foreach ( $coords AS $username => $location ) {
$num = count($location) - 1;
$kml .= '
   <Placemark>
   <name>'.$username.'</name>
   <Point>
   <coordinates>'.$location[$num]['lon'].','.$location[$num]['lat'].',0.0</coordinates>
   </Point>
   </Placemark>';
}

$kml .='  </Document>
</kml>
';
Header('Content-Type: application/vnd.google-earth.kml+xml');
echo $kml;
?>

This is the KML file, which you open with Google Earth.

Trails Hollywood Bowling

Trails Hollywood Bowling

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Folder>
    <name>Your Testlab</name>
    <visibility>1</visibility>
    <open>0</open>
    <description>You are experimenting with GPS and iPhone</description>
    <NetworkLink>
      <name>Selected Placemarks</name>
      <visibility>1</visibility>
      <open>1</open>
      <description>These are generated and refreshed automagically</description>
      <refreshVisibility>1</refreshVisibility>
      <flyToView>1</flyToView>
      <Link>
				<refreshMode>onInterval</refreshMode>
				<refreshInterval>30</refreshInterval>
        <href>http://www.klammeraffe.org/kmlexporter.php</href>
      </Link>
    </NetworkLink>
  </Folder>
</kml>

Have fun.