How to display your accurate google latitude location address with the public badge map in your blog

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:

8 thoughts on “How to display your accurate google latitude location address with the public badge map in your blog

  1. Alex

    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….

    Reply
  2. Pingback: Accurate Google Latitude - LATEST WEB TRENDS- ONLINE SEARCHES-GOOGLE SEARCH TRENDS – LATEST WEB TRENDS- ONLINE SEARCHES-GOOGLE SEARCH TRENDS

  3. Simo

    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

    Reply
  4. Mahesh

    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; ?>

    Reply
  5. escort service in nyc

    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

    Reply

Leave a Reply to Simo Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>