Google latitude is a great way to share your current GPS location with everyone. You can put up the google location badge on your blog and let the world know your exact geographical location.
But the latitude badge does not provide the exact address of your current location in terms of the area names but shows the exact location on the map. Google latitude badge shows only city level address along with the map. So I decided to write my own small PHP script to print out the exact address instead of an approximate one which is shown by default. You could use this code coupled with XMPP integration to show it as you google talk status message (a tutorial and script for this..sometime later)
Basically, instead of displaying “<City name>,<country>”, this script will display “<Road name>, <areaname>,<cityname>,<country>”
The internals : As soon as the script is executed, It uses the google latitude developer api to get your current co ordinates, then it makes use of the google maps v3 geocoding api (previously called reverse geo coding) to get the info about the co-ordinates of your current location which includes the street name/road name etc.
Steps to add this cool thingy onto your own site:
1. Install google maps application on your GPS mobile phone having GPRS connection (symbian s60,iphone,android) and then enable Latitude inside google maps app. Ensure that detect location is selected inside the latitude settings under google maps.
2. Goto google latitude web page (http://www.google.com/latitude/apps/badge) and select Enable and show best available location.
3. In the javascript embed code section of the badge config page, select zoom level automatic, and choose the map type, size of map.
4. Put the generated embed code on your blog to display the map and an approx city level address (we’ll fix this in a moment)
5. Now to display a more accurate address (which includes the roadname,streetnames, areanames) , extract the userid for latitude from the javascript embed code and put it in the below PHP code (line 2)
6.Add this PHP code into a file named “mylatitudelocation.php” and upload it into your webserver root.
7. Add this iframe code into your blog widgets sections to display the accurate address of your current location
<iframe src="mylatitudelocation.php" width="100%" height="50" frameborder="0"></iframe>
Here’s the PHP code:
<?php $latitudeUserid=”-4xxxxx….” // change this and insert your latitude user id (available from the public badge embed code ”
function ago($tm,$rcs = 0) { $cur_tm = time(); $dif = $cur_tm-$tm; $pds = array(‘second’,'minute’,'hour’,'day’,'week’,'month’,'year’,'decade’); $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600); for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v–); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]); $no = floor($no); if($no <> 1) $pds[$v] .=’s'; $x=sprintf(“%d %s “,$no,$pds[$v]); if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm); return $x; }
$latitudeAPI=”http://www.google.com/latitude/apps/badge/api?user=$latitudeUserId&type=json”;
$locationDetails=json_decode(file_get_contents($latitudeAPI)); $lat=$locationDetails->features[0]->geometry->coordinates[0]; $lng=$locationDetails->features[0]->geometry->coordinates[1]; $timestamp=$locationDetails->features[0]->properties->timeStamp; $when=ago($timestamp);
$latlng=”$lng,$lat”;
$geocodeAPI=”http://maps.google.com/maps/api/geocode/json?latlng=$latlng&sensor=false”;
$locationDetails=json_decode(file_get_contents($geocodeAPI)); $locationString=$locationDetails->results[0]->formatted_address;
echo “$when ago, I was in $locationString”; ?>
Incoming search terms:
- how accurate is google latitude (44)
- maps google com latittude userid (17)
- javascript latitude to address (8)
- how accurate is google latitude? (7)
- google latitude show coordinates (6)
- google latitude get location by userid (5)
- how to insert name location in google map latitude (5)
- how accurate is google lattitude (4)
- google latitude php script (4)
- show your location on google map on your website (3)
Thanks man
but you probably should put a notice about thoe auto curly quotes in wordpress.
hey im trying to figure out if i can just get the city, state and time updated and nothing else. By that I mean no map and no other text. Any idea how i could do that? My server is a windows box with iis….
Pingback: Accurate Google Latitude - LATEST WEB TRENDS- ONLINE SEARCHES-GOOGLE SEARCH TRENDS – LATEST WEB TRENDS- ONLINE SEARCHES-GOOGLE SEARCH TRENDS
Thanks for the code, but it seems to be a bit broken.
I know it’s been a while but is there any chance you could clean it up and post it with the correct formatting so that a PHP amateur like me can make it work?
As mentioned above, i think your blog has changed some of your quote formatting.
There’s also a missing semicolon at the end of the line for the first variable $latitudeUserid
Thanks,
~Simo
Finally got it working, this is what that worked.
= 0)&&(($no = $dif/$lngh[$v])<=1); $v=$v-1)
if($v < 0) $v = 0;
$_tm = $cur_tm-($dif%$lngh[$v]);
$no = floor($no); if($no 1) $pds[$v] .='s';
$x=sprintf("%d %s ",$no,$pds[$v]);
if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= time_ago($_tm);
return $x; }
$latitudeAPI="http://www.google.com/latitude/apps/badge/api?user=".$latitudeUserId."&type=json";
$locationDetails=json_decode(file_get_contents($latitudeAPI)); $lat=$locationDetails->features[0]->geometry->coordinates[0]; $lng=$locationDetails->features[0]->geometry->coordinates[1]; $timestamp=$locationDetails->features[0]->properties->timeStamp; $when=ago($timestamp);
$latlng=$lng.",".$lat;
//$latlng=$lat.",".$lng;
$geocodeAPI="http://maps.google.com/maps/api/geocode/json?latlng=".$latlng."&sensor=false";
$locationDetails=json_decode(file_get_contents($geocodeAPI)); $locationString=$locationDetails->results[0]->formatted_address;
echo $when." ago, I was in ".$locationString; ?>
Hello! This is kind of off topic but I need some help from an established blog.
Is it very difficult to set up your own blog?
I’m not very techincal but I can figure things out pretty fast. I’m thinking
about setting up my own but I’m not sure where to begin. Do you have any points or suggestions? Cheers
This post is really a nice one it assists new the web visitors, who are wishing in favor of blogging.