Thursday, May 5, 2011

Easier way to use Google Latitude API : Google Badge API



Tweet #newtonball with a screenshot of the level to win a suprize gift.
Checkout this cool game
Newton Ball in Playstore








There are two ways in which you can use the Google Latitude API

a) Use the long way in which you will need the user to authenticate
your Google Latitude App each time you want to get his location
details, but this rather a long method to get the user's location.
Its a more generalized & accepted method of using the Google
Latitude API. The disadvantages of this method is that you can
create a vehicle tracking system or human tracking system using
this methodology since you need to follow the steps in the
OAUTH playground to authenticate your application to access the
location details .

b) Google Latitude Hack :
We hereby comeup with a simpler and a much easier method to use
Google Latitude API and we are calling it as Google Latitude
HACK since it simplifies the location tracking process & its
hardly few lines of code .

Steps for using Google Latitude Badge API .

We will call this technique as Google Latitude Badge API ,why?
Because it uses the Google Latitude + Google Badge for location
tracking purposes .

a) Log into your Google Account in which you have Google
Latitude enabled .( Enabling Google Latitude is simple
you just have to log into your Google Latitude using
any of your Google Accounts and you will automatically
enable it ).

b) Goto https://www.google.com/latitude/b/0/apps .This is
nothing but your Google Latitude Apps settings section.

c) The next step is to enable your public location badge .
    In Google Public Location Badge choose the best
    location badge radio button .

d) If you are following this steps using a A-GPS enabled
    mobile phone , your location will displayed on the map
    as  shown below .

 e) If you following this
 steps using a pc you may
set a static location of your
choice on Google Latitude.
For that Enable the Set My
Location Manually option
under Privacy Section in
Friends  Tab .    
   









f) So once you have followed this basic steps you are
ready to implement the location tracking system using
Google Latitude Badge API .

Code :
<?php
//Replace the YOURUSERID in the url below with your Badge userid . 
$url="http://www.google.com/latitude/apps/badge/api?user=YOURUSERID&type=json" ;
// We get the content

$content = file_get_contents( $url );

// We convert the JSON to an object
$json = json_decode( $content );
$coord = $json->features[0]->geometry->coordinates;
// this will give you the coordinates of the user
echo $coord ;

$timeStamp = $json->features[0]->properties->timeStamp;
// timestamp of the last update

echo $timestamp ;
// If you want to retrieve the longitude & latitude separately 

$latitude=$coord[0];

$longitude=$coord[1];

echo "Your current coordinates  Latitude :".$latitude."  Longitude".$longitude ;

?>


g) How do I get the userID?

Under the Google Latitude Badge a small code similar to as
shown below is seen .

<iframe frameborder="0" height="300" src="http://www.google.co.in/latitude/apps/badge/api?user=XXXXXXXXXXXX&amp;type=iframe&amp;maptype=roadmap&amp;hl=en-GB" width="180">
</iframe>
  
So the XXXXXXXX is your userID replace that in your code and
you can have your code running .

11 comments

code mistake but no problem:

echo $coord ; wont work
you can use this :
echo "[", $coord[0], ", ", $coord[1], "]";
it will gives out the array...
echo $timestamp ; wont work too because you can change it to:
echo $timeStamp ;
and it will be work ;)

lovly greets from germany, Crazyboy1

Thank you! I couldn't get Latitude's API to work with Apps Script, but this hack solved the problem :)

Assuming i am viewing 6 friends' locations, will this method allow me to embed THAT map onto my website... so that the viewers sees my friends' locations as well?

Yes Scott if you can get 6 of your friends userId then you can get
each of your friends location and show them on map .

How can i get my friends public badge user id ?

No U cant get your friends public badge user id unless you ask him .... not through the API

I am not very handy with php, but I embedded this piece of code into an htm page and all i get back is that piece of code, I could use some help on how the script should look like to see me and some friends on my webpage.
(I am trying with 4 user badges, but I dont see a map, only code)

This comment has been removed by the author.

I assume it's not possible to set your current location through this badge method?

My phone was stolen can you help me use this code to locate it better I have Google latitude installed

I've been using something similar to print out just the location in text with this code:

$txt = "Latitude current location code";
/* This line creates the variable $txt and gives it the initial value. Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. */

$info = file_get_contents("http://www.google.com/latitude/apps/badge/api?user=" your google ID "&type=json");
$latitude=json_decode($info,true);
$place=$latitude["features"]["0"]["properties"]["reverseGeocode"];
$timestamp=$latitude["features"]["0"]["properties"]["timeStamp"];


echo "$place (".getRelativeTime($timestamp).")";

function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}?