Access your Google Latitude position from a .Net app

When I saw that Google Latitude now enables you to access your data by a JSON feed, I decided to make it communicate with a little GPS tracking project of mine.

I’m really found of all these ways we now have to make anything communicate with anything. You can build interfaces from any system to any other system really easily.

This code enables you to automatically get your GPS position (or the position of a friend) from your JSON Latitude feed. To be able to do that, you have to enable your KML/JSON feed.
It requires .Net 3.5′s System.Web.Extensions

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
 
namespace LatitudeReader {
	class Program {
		static void Main() {
 
			Console.WriteLine( "What is your user id ?" );
 
			var userId = Console.ReadLine();
 
			if ( userId == String.Empty )
				userId = "5616045291659744796";
 
			// Url of the JSON Latitude feed
			var url = String.Format( "http://www.google.com/latitude/apps/badge/api?user={0}&type=json", userId );
 
			// We download the file
			var ms = new MemoryStream();
			Download( url, ms );
 
			// JSON in text format
			var textContent = UTF8Encoding.UTF8.GetString( ms.ToArray() );
 
			// We convert the JSON text file to an object
			// It returns 
			var jss = new JavaScriptSerializer();
			var jsonContent = jss.DeserializeObject( textContent ) as Dictionary<String, Object>;
 
			// We get the data
			var features = ( jsonContent[ "features" ] as object[] )[ 0 ] as Dictionary<string, object>;
			var geometry = features[ "geometry" ] as Dictionary<string, object>;
			var coordinates = geometry[ "coordinates" ] as object[];
			var lon = coordinates[ 0 ] as decimal?;
			var lat = coordinates[ 1 ] as decimal?;
 
			// And then the timestamp
			var properties = features[ "properties" ] as Dictionary<string, object>;
			var date = ConvertFromUnixTimestamp( (double) (int) properties[ "timeStamp" ] );
 
			// We convert the UTC date to local time
			date = date.ToLocalTime();
 
			Console.WriteLine( "{0} : {1} x {2}", date, lat, lon );
		}
 
		public static DateTime ConvertFromUnixTimestamp( double timestamp ) {
			DateTime origin = new DateTime( 1970, 1, 1, 0, 0, 0, 0 );
			return origin.AddSeconds( timestamp );
		}
 
		private const int BUFFER_SIZE = 1024;
 
		private static void Download( string url, Stream writeStream ) {
			var request = (HttpWebRequest) WebRequest.Create( url );
			var response = request.GetResponse();
 
			var readStream = response.GetResponseStream();
 
			var data = new Byte[ BUFFER_SIZE ];
 
			int n;
			do {
				n = readStream.Read( data, 0, BUFFER_SIZE );
				writeStream.Write( data, 0, n );
			} while ( n > 0 );
 
			writeStream.Flush();
			readStream.Close();
		}
	}
}

The only references you need are : System and System.Web.Extensions

GD Star Rating
loading...
Access your Google Latitude position from a .Net app, 8.7 out of 10 based on 3 ratings

10 Responses to “Access your Google Latitude position from a .Net app”

  1. Jo Gabriel Says:

    Hi Florent,

    In your example , you retrieve location info from your GPS tracking project in a .net application.
    That’s clear to me but I was wondering how the GPS tracker is broadcasting its position to the google latitude servers?

    Regards,
    Jo

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

    Well. I wasn’t clear, my tracking project works with some proprietary software. It only gets the position from Google Latitude as an auxiliary bonus feature. It doesn’t publish anything on Google Latitude.

    GD Star Rating
    loading...
  3. » Google Latitude History Florent Clairambault Says:

    [...] data so that we could see where we were at a specific time.” And I very quickly published some code to get my position (you could be tracking me right now with it). I thought they would never release the history saving [...]

  4. mayooresan Says:

    thanks a lot. This is wot i’m looking for. Gonna re-use this code with my touch for my final year project :)

    GD Star Rating
    loading...
  5. Embed Google Maps in C# Application | Mayu’s IT Diary Says:

    [...] First check out this post to see how you can retreive the GPS location of your device. [...]

  6. Gaurav Says:

    HOw to get the user id you have used in the above sample

    GD Star Rating
    loading...
  7. janaka Says:

    i am using .net 2.0 frame work so i tried to make a 2.0 supportable version but couldn’t please help me to run this project on 2.0 because this one exactly full filling my need

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

    @janaka
    .Net 3.5 is a set of additionnal classes on top of the same .Net 2.0 runtime. So you can just add the System.Web.Extensions assembly with your current runtime and it will work.

    I definitely don’t have time to offer you an other solution.

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

    Click on the link I gave in the post, look at public location badge iframe src attribute. This is a link that contains a “user” parameter. This is it.

    GD Star Rating
    loading...
  10. MarkE Says:

    Hey!

    I like it,
    but I dont see where i can access my account and get my friends position. Id like to be able to do that so i can flag when my lift share is at the point i need to leave, and when friends are close.

    thanks!

    GD Star Rating
    loading...

Leave a Reply