View Single Post
Posts: 94 | Thanked: 40 times | Joined on Jun 2010 @ Germany
#4
@digitalviod: In short it is like this:
python script:
Code:
*snip the whole gps and internet stuff*
    if device.fix:
        if device.fix[1] & location.GPS_DEVICE_LATLONG_SET:
            print "lat = %f, long = %f" % device.fix[4:6]
            url= "http://server.de/gps/upload.php?lat=%f&lon=%f&key=XXX" % device.fix[4:6]
            print url
            req = urllib2.Request(url)
            reply = urllib2.urlopen(req)
PHP script stores to SQL:
PHP Code:
<?php
$db
= new mysqli('localhost''root''root''test');
if (
mysqli_connect_errno()) {
    die (
'Konnte keine Verbindung zur Datenbank aufbauen: '.mysqli_connect_error().'('.mysqli_connect_errno().')');
}

$sql='INSERT INTO
    gps(lat, lon, date)
VALUES
    ('
.$_GET['lat'].',
     '
.$_GET['lon'].',
    NOW());'
;
$result $db->query($sql);
if (!
$result) {
    die (
'Etwas stimmte mit dem Query nicht: '.$db->error);
}
?>
kml-php-script:
PHP Code:
<?php
$db
= new mysqli('localhost''root''root''test');
if (
mysqli_connect_errno()) {
    die (
'Konnte keine Verbindung zur Datenbank aufbauen: '.mysqli_connect_error().'('.mysqli_connect_errno().')');
}

$sql="SELECT
    lat,
    lon,
    DATE_FORMAT(date, '%Y-%m-%dT%H:%i:%sZ') as newdate
FROM
    gps
ORDER BY date DESC LIMIT 1"
;
$result $db->query($sql);
if (!
$result) {
    die (
'Etwas stimmte mit dem Query nicht: '.$db->error);
}
$row $result->fetch_assoc();
echo 
'<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
    <name>KmlFile</name>
     <Style id="bike-icon">
      <IconStyle>
        <Icon>
          <href>http://server.de/gps/bike.png</href>
        </Icon>
      </IconStyle>
    </Style>'
;
$sql="SELECT
    lat,
    lon,
    DATE_FORMAT(date, '%Y-%m-%dT%H:%i:%sZ') as newdate
FROM
    gps
ORDER BY date DESC LIMIT 1"
;
$result $db->query($sql);
if (!
$result) {
    die (
'Etwas stimmte mit dem Query nicht: '.$db->error);
}
while (
$row $result->fetch_assoc()) {
echo 
'<Placemark>
    <name>'
.$row['newdate'].'</name>
    <description>Hier wurde die letzte Position gesendet.</description>
    <Point>
      <coordinates>'
.$row['lon'].','.$row['lat'].',0</coordinates>
    </Point>
        <!--<TimeStamp>
        <when>'
.$row['newdate'].'</when>
        </TimeStamp>-->
        <styleUrl>#bike-icon</styleUrl>
  </Placemark>
  '
;
}
echo 
'</Document>
</kml>'
;
?>
The sql query is double because I had other stuff in this file before which I removed and I forgot the second query. The TimeStamp is commented because the timeline was a bit annoying when new data came in. So this script creates a xml-compatible file which is referenced by the following .kml-file on the local-harddrive:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
    <NetworkLink>
      <name>Mein N900</name>
      <visibility>1</visibility>
      <open>1</open>
      <description>N900 On Tour</description>
      <refreshVisibility>0</refreshVisibility>
      <flyToView>1</flyToView>
      <Link>
        <href>http://server.de/gps/lastpos.php?key=XXX</href>
		<refreshMode>onInterval</refreshMode>
		<refreshInterval>30</refreshInterval>
      </Link>
    </NetworkLink>
</kml>
This structure could also be used to display a embedded Google Map on the webserver as they can parse kml files, too. I know the code is messy but my first goal was to get the information from my phone to Google Earth. Outsourcing the SQL functions into include files and a general cleanup will follow.
So you are using python on the server, too. Is this available on many hosters? I'm only used to php on servers and have zero experience with anything else.

@handaxe: I searched for Iamhere but couldn't find any reliable (recent) info at all. Also I don't want it with SMS as you'd need the phone's number and this script will upload the location as soon as it is connected to any internet connection no matter which sim is inserted. It's still mostly a simple project to learn something about python, maemo, php and the kml files