Scrupuli

blunt essays with sharp points

Make Firefox Take Geolocation From Google Latitude

by Scrvpvlvs
Nov 29, 2010 5:16 AM–Firefox takes its geolocation from Google Latitude, with the help of a small PHP program.

Websites can now ask for your location, and a few do. For example, Gowalla wants your location so that it can show you nearby places of interest.

When you give your permission, your browser does its best to find out where it is and let the website know. But sometimes this procedure goes wrong. For example, my home computer is in Tarrant County, Texas, but my browser tells Gowalla that it is miles away in Dallas County.

Now, if you know your actual latitude and longitude, there are a couple of ingenious ways that you can supply this to the Firefox browser so that it can offer the correct location to websites. But these methods, which have been blogged about elsewhere, have two drawbacks: First, there are several manual steps. You must find out your latitude and longitude, and then you must carefully update your browser with the information. Second, you must do this each time your location changes.

But Google Latitude lets me simply click on a map to set my exact location. So I came up with a refinement of the manual method. I arranged for Firefox to ask Google Latitude for my location.

First, I built a small web page which finds out my Google Latitude location and supplies it to Firefox when asked. For the time being, I have made this web page public so anyone can use it. If it becomes so popular that my web server cannot handle the demand, I will restrict its use. I hope other people will also make this web page public on their servers, so I am reproducing the page source code at the end of this article.

Second, I made my location public in Google Latitude using the Google Public Location Badge option. From the code snippet for the Badge, I copied my user code, which in my case was a large negative number.

Third, I opened about:config in Firefox, and changed the value of geo.wifi.uri from the default value to http://archweaver.com/geol?user= and pasted my user code after the equal sign.

And that was it. Now, when I click to set my Google Latitude location, Firefox passes that location on to websites such as Gowalla.

Incidentally, when I want to restore the default behavior, I simply reopen about:config, right-click on geo.wifi.uri, and choose Reset.

Here is the PHP code mentioned above:

 1 <?php
 2 // geol.php - pass Google Latitude location to Firefox.
 3 // Author: MetaEd. Update: 2011-12-21.
 4 
 5 // Identify this program's output as JSON.
 6 header('Content-type: application/json');
 7 
 8 // Get Google Latitude Badge as JSON, using the key provided. The
 9 // cast to integer protects the URI from attack.
10 $badgeKey = (int) $_GET['user'] ;
11 $badgeUri = 'http://www.google.com/latitude/apps/badge/api?user='
12   . $badgeKey
13   . '&type=json' ;
14 $badgeJson = file_get_contents( $badgeUri ) ;
15 $badgeObject = json_decode( $badgeJson ) ;
16 
17 // Read the latitude and longitude from the badge.
18 $longitude = $badgeObject->{'features'}[0]->{'geometry'}
19   ->{'coordinates'}[0] ;
20 $latitude = $badgeObject->{'features'}[0]->{'geometry'}
21   ->{'coordinates'}[1] ;
22 $accuracy = $badgeObject->{'features'}[0]->{'properties'}
23   ->{'accuracyInMeters'} ;
24 
25 // Encode the geolocation in JSON and write it to Firefox.
26 // There are two encodings. Firefox 8 and older request the old 
27 // encoding using the POST method; Firefox 9 and newer request the 
28 // new encoding using the GET method.
29 switch($_SERVER['REQUEST_METHOD'])
30 {
31   case 'POST' : // Firefox 8 and older
32   $geoArray = array(
33     'location'=>array(
34       'latitude'=>$latitude
35         ,
36       'longitude'=>$longitude
37         ,
38       'accuracy'=>$accuracy
39     )
40   ) ;
41   $geoJson = json_encode( $geoArray ) ;
42   break ;
43 
44   case 'GET' : // Firefox 9 and newer
45   $geoArray = array(
46     'status'=>'OK'
47       ,
48     'accuracy'=>$accuracy
49       ,
50     'location'=>array( 'lat'=>$latitude , 'lng'=>$longitude )
51   ) ;
52   break ;
53 }
54 
55 $geoJson = json_encode( $geoArray ) ;
56 echo $geoJson ;
57 ?>

I want to be very plain that this work is preliminary. I could do a much better job of explaining the step by step instructions for setting this up. I could also do a much better job of input validation and exception handling. But I wanted to get the word out that this is really very easy to do, and get some real world feedback that would direct my efforts.

So if you give it a try, please comment here. Mention any trouble you run into, and also let me know what you are using it for.

Cheers!

Labels: , , , , , , ,

(go to complete article)

Share:

15 comments

about.me

Follow

feed

E-mail: enter address

Project Euler competitor metaed

vs.

Project Euler competitor db8

profile for MetaEd on Stack Exchange, a network of free, community-driven Q&A sites

Recent Articles

Cataclysm

Open letter re: Grinnell College alumni “lifetime”...

Spybot – Search & Destroy interferes with Lync 201...

A moment of silence

Rondeau

Howard Schultz of Starbucks: firm on support for m...

In each of us, two natures are at war

Clorox does not understand how to measure bleach

This season’s pie recipe

Adamah

Archives

November 1999
June 2000
July 2000
September 2001
October 2001
February 2002
March 2002
June 2003
February 2004
June 2004
July 2004
August 2004
September 2004
February 2005
March 2005
November 2005
July 2007
March 2008
April 2008
May 2008
October 2008
November 2008
December 2008
January 2009
April 2009
September 2009
December 2009
February 2010
March 2010
May 2010
June 2010
September 2010
October 2010
November 2010
December 2010
January 2011
April 2011
June 2011
July 2011
August 2011
September 2011
December 2011
February 2012
April 2012
May 2012
June 2012
July 2012
August 2012
September 2012
November 2012
January 2013
February 2013
April 2013
February 2014
May 2014
October 2014
June 2017
February 2019