This is just an ugly example.
Demo: http://martin.stoll.se/maps/location.php
Code available here: https://bitbucket.org/oller/latitude-history-example/
What you need to follow my example
Create a table in your database to store data, table.sql:
CREATE TABLE IF NOT EXISTS `history` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`latlng1` varchar(15) NOT NULL,
`latlng2` varchar(15) NOT NULL,
`accuracyInMeters` int(11) NOT NULL,
`timeStamp` int(11) NOT NULL,
`reverseGeocode` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
save-position.php – backend script, get your current position and store it in the table
Create a cronjob if you want to update data automatically.
<?php
include "config.php";
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
// Userspecific URL. https://www.google.com/latitude/b/0/apps
// Replace 1234567890 with your own key
$jsonurl = "http://www.google.com/latitude/apps/badge/api?user=1234567890&type=json";
$json = file_get_contents($jsonurl,0,null,null);
// converts it into a PHP variable (array)
$jsonarr = json_decode($json, true);
$LatLng1 = $jsonarr['features'][0]['geometry']['coordinates'][0];
$LatLng2 = $jsonarr['features'][0]['geometry']['coordinates'][1];
$reverseGeocode = $jsonarr['features'][0]['properties']['reverseGeocode'];
$timestamp = $jsonarr['features'][0]['properties']['timeStamp'];
$accuracyInMeters = $jsonarr['features'][0]['properties']['accuracyInMeters'];
// Get last location from database. This is optional
$sql = "SELECT latlng1, latlng2 FROM history ORDER BY id DESC LIMIT 1";
$res = mysql_query($sql, $link) or die(mysql_error($link));
$row = mysql_fetch_row($res);
// Store it if its different
if($row[0] != $LatLng1 && $row[1] != $LatLng2){
$sql = "INSERT INTO history(latlng1, latlng2, accuracyInMeters, timeStamp, reverseGeocode)
VALUES($LatLng1, $LatLng2, $accuracyInMeters, $timestamp, '$reverseGeocode')";
$res = mysql_query($sql, $link) or die(mysql_error($link)."<br />$sql");
print "Data saved. $sql";
} else {
print "No change. Dont save.";
}
?>
font-end.php
<?php
include "config.php";
$accuracyInMeters = 500; // accuracy in meters
$antalPositioner = 20; // how many positions to show
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Qvarting - Google latitude</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize(){
<?php
// Hämtar den senaste positionen (Ska vara annan färg på den)
$sql = "SELECT latlng2, latlng1, accuracyInMeters, timeStamp, reverseGeocode FROM history WHERE accuracyInMeters < $accuracyInMeters ORDER BY id DESC LIMIT 1";
$res = mysql_query($sql, $link) or die(mysql_error($link));
$antal = mysql_num_rows($res);
$row = mysql_fetch_row($res);
$tid = date('Y-m-d H:i', $row[3]);
print "var myLatlng = new google.maps.LatLng($row[0], $row[1]);\n";
?>
var myOptions = {
zoom: 5,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<?php
print "var marker = new google.maps.Marker({\n";
print " position: myLatlng,\n";
print " map: map,\n";
print " title: '".utf8_encode($row[4])." - $tid'\n";
print "});\n";
print "var infowindow = new google.maps.InfoWindow({\n";
print " content: '$tid<br />".utf8_encode($row[4])."'\n";
print "});\n";
print "google.maps.event.addListener(marker, 'click', function() {\n";
print " infowindow.open(map,marker);\n";
print "});\n";
?>
<?php
// Hämtar de 20 senaste positionerna förutom den senaste (den hämtas ovanför)
// yea, pretty ugly. i know. put it in javascript arrays instead.
$sql = "SELECT latlng2, latlng1, accuracyInMeters, timeStamp, reverseGeocode FROM history WHERE accuracyInMeters < $accuracyInMeters ORDER BY id DESC LIMIT 1,$antalPositioner";
$res = mysql_query($sql, $link) or die(mysql_error($link));
$antal = mysql_num_rows($res);
$i = 1;
while($row = mysql_fetch_row($res)){
$tid = date('Y-m-d H:i', $row[3]);
print "var myLatlng".$i." = new google.maps.LatLng($row[0], $row[1]);\n";
print "var marker".$i." = new google.maps.Marker({\n";
print " position: myLatlng".$i.",\n";
print " map: map,\n";
print " icon: 'pointer.png',\n";
print " title: '".utf8_encode($row[4])." - $tid'\n";
print "});\n";
print "var infowindow$i = new google.maps.InfoWindow({\n";
print " content: '$tid<br />".utf8_encode($row[4])."'\n";
print "});\n";
print "google.maps.event.addListener(marker$i, 'click', function() {\n";
print " infowindow$i.open(map,marker$i);\n";
print "});\n";
$i++;
}
?>
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="float: left; width: 70%;"></div>
<div style="float: left; width: 30%; font-family: Verdana; font-size: 10px">
<?php
$sql = "SELECT latlng2, latlng1, accuracyInMeters, timeStamp, reverseGeocode FROM history WHERE accuracyInMeters < $accuracyInMeters ORDER BY id DESC LIMIT $antalPositioner";
$res = mysql_query($sql, $link) or die(mysql_error($link));
while($row = mysql_fetch_row($res)){
print date('Y-m-d H:i', $row[3])."<br />".utf8_encode($row[4])."<br /><br />";
}
?>
</div>
</body>
</html>
config.php
Connect to database
<?php
$DBhost = "localhost";
$DBuser = "user";
$DBpass = "pass";
$DB = "database";
$link = mysql_connect($DBhost, $DBuser, $DBpass);
mysql_select_db($DB);
?>