Access your Google Latitude position from PHP

The code to access your Google Latitude position is even simpler in PHP than it is in .Net :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?PHP
header('Content-Type: text/plain');
 
$userId = '5616045291659744796';
 
if ( $_GET['user'] ) {
	if ( is_numeric( $_GET['user'] ) )
		$userId = $_GET['user'];
	else
		exit('This isn\'t a valid user id.');
}
 
$url = 'http://www.google.com/latitude/apps/badge/api?user='.$userId.'&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;
$timeStamp = $json->features[0]->properties->timeStamp;
 
if ( ! $coord ) 
	exit('This user doesn\'t exist.');
 
$date = date( 'd/m/Y H:i:s', $timeStamp );
$lat = $coord[1];
$lon = $coord[0];
 
echo $date.' : '.$lat.' x '.$lon;
?>

This program is available for testing here. It requires PHP 5.2.0 to run the json_decode method.

I think this is the power of PHP, you can make some powerful code in no time. The drawback is that it’s really slow, and it becomes even slower if you begin to use heavy objects (and objects are often heavy). And I personally think it’s much easier and safer to debug and maintain complex .Net programs than complex PHP programs.

By the way, you can generate your google badge userid that you can then use for the API here. (thank you Neavilag for this comment)

GD Star Rating
loading...
Access your Google Latitude position from PHP, 8.3 out of 10 based on 7 ratings

11 Responses to “Access your Google Latitude position from PHP”

  1. Jérôme Fafchamps Says:

    Pas mal comme snippet, quelques lignes… mais utile ! Merci ;)

    GD Star Rating
    loading...
  2. neavilag Says:

    Thanks for the post, how can i know my userid or any of my friends ?

    regards

    GD Star Rating
    loading...
  3. Florent Clairambault Says:

    Hi Neavilag,

    Your usedid is generated for your public google badge here : https://www.google.com/latitude/apps/badge .
    But you don’t have to use it publicly, you can keep this userid to yourself.

    GD Star Rating
    loading...
  4. neavilag Says:

    Thanks Florent… :-) )) already there…

    GD Star Rating
    loading...
  5. DT Says:

    Hey Florent,
    finally a straightforward example of how to get into Latitude’s api using php – thank you!

    The reason I was looking for this was because Latitude’s map isn’t right for what I need (user can’t zoom/drag, and the badge doesn’t look that great design-wise). So I wanted to just grab the coords from latitude and input it into a regular google map interface.

    So I put this together (inputting the coords into a wp plugin called Simple Google Map), however the location on the regular google map is coming out slightly differently to the one on Latitude, it’s about half a km out of place.

    Would you happen to know why? I know it’s not the topic of this article, but I see you’re fairly on top of this stuff.

    Many thanks

    GD Star Rating
    loading...
  6. Florent Clairambault Says:

    Hi DT,

    1. You can actually add zoom/drag to Google Maps. You just have to do it yourself. This is what I did here.

    2. I can’t really tell you what happened. I tested the google maps webservice here and inputed the generated coords in google maps and the point was perfectly placed.

    Best regards,

    Florent

    GD Star Rating
    loading...
  7. piyush garg Says:

    hiii…
    i am using your code ,, but i am confused in user id…so what is user id for me..
    please reply

    GD Star Rating
    loading...
  8. Florent Clairambault Says:

    You should read the first article about this first: http://florent.clairambault.fr/access-your-google-latitude-position-from-a-net-app

    GD Star Rating
    loading...
  9. Norberto Says:

    Thank you very much for this code. It really helped me out to create a custom badge for my Google Latitude account.

    Nonetheless, I would like to go a little bit further. Instead of having access only to the latest location, I would like to access my Latitude History, which is private and is never disclosed with any badge approach.

    Are you familiar with the Latitude API? Basically I want to use this API to access the location history of a specific Google Account with Latitude enabled (given the necessary keys and so forth) and plot them on a Google Map.

    Plotting points on a Google Map with its API is pretty straightforward, but I still didn’t find any way to do the other part (fetch the location history).

    Can you give me some hints?

    GD Star Rating
    loading...
  10. Florent Clairambault Says:

    Sorry, I’m not familiar with it and I really don’t have the time to look into it.

    GD Star Rating
    loading...
  11. Mark Says:

    I did a similar thing on (http://www.marksmayo.com/where-is-mark/) but while it’s showing my location just fine, I can’t get it to display the Latitude badge picture, which would be nicer than just a bubble. Thoughts?

    GD Star Rating
    loading...

Leave a Reply