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: , , , , , , ,

Share: submit to reddit

15 Comments:

by Anonymous Anonymous
December 01, 2010 8:42 AM–I fetched a copy of the php and put it on my server; it seems to work just fine!

What are you using to update Google Latitude? I have an iPhone, so I run an app called "Latitudie" that will update my position every 15 minutes (configurable). I use this as one of the triggers for which phones to activate with my Google Voice account (i.e., when I'm at home, it'll forward calls there).  

by Blogger Scrvpvlvs
December 01, 2010 2:03 PM–At http://google.com/latitude I either enter an address or simply click to update my position.  

by Blogger gowtham
July 23, 2011 11:06 PM–is ur site public still>>>  

by Blogger Scrvpvlvs
July 27, 2011 4:25 PM–Yes, and a small number of people are using it.  

by Anonymous Anonymous
December 21, 2011 9:30 AM–It seems that your script doesn't work with the latest versions of Firefox.  

by Blogger Scrvpvlvs
December 21, 2011 1:26 PM–Can you post the version you're having trouble with? It still works for me. I tested it with Firefox 8.0.1, released 21 November 2011.  

by Anonymous Anonymous
December 21, 2011 2:06 PM–Hi, I'm using Firefox 9.0 on Ubuntu. Your script doesn't work, and neither does geomena.org.

The default geolocation uri was chaged from https://www.google.com/loc/json to https://maps.googleapis.com/maps/api/browserlocation/json , maybe the protocol the Firefox expect has changed as well?

See also:
https://bugzilla.mozilla.org/show_bug.cgi?id=689252  

by Blogger Scrvpvlvs
December 21, 2011 4:46 PM–Thank you for calling this to my attention. Sure enough, Firefox 9 is out and the protocol for returning the geolocation has changed. See: Bug 677256 - Migrate to Google Location Service v2.

I have updated the code to add support for Firefox 9, and updated the article accordingly.  

by Anonymous Anonymous
December 21, 2011 6:10 PM–Thanks! Now the script works for me with Firefox 9.  

by Anonymous Anonymous
January 07, 2012 7:46 PM–It looks like there are more people struggling with the new API:

https://bugzilla.mozilla.org/show_bug.cgi?id=713304

Maybe you can present you elegant solution to the compatibility problem there.  

by Anonymous Anonymous
March 08, 2012 10:48 PM–Hi,

It looks like archweaver.com is down:

curl -v "http://archweaver.com/geol?user=1234"
* About to connect() to archweaver.com port 80 (#0)
* Trying 174.143.212.206... Connection timed out
* Trying 2001:470:1f0e:e16::2... Connection timed out
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host  

by Blogger Scrvpvlvs
March 09, 2012 11:32 AM–My ISP performed an unplanned server reboot right around the time you had the problem. It came back up a few minutes later.  

by Anonymous Elad
July 17, 2013 10:38 AM–Google is about to shut down Latitude, so unfortunately this trick will no longer work.

https://support.google.com/gmm/answer/3001634  

by Anonymous Anonymous
October 21, 2015 8:58 PM–Kind of New to all this so i paste http://archweaver.com/geol?user= in the geo.wifi.url but what do i put after the = ?

for example say i wanted to pick Statue of Liberty in New York.
would i type

http://archweaver.com/geol?user=(Lat40.68911476887273,Lon-74.04449999999999)

be great if you guys could help me out here :-)
 

by Blogger Scrvpvlvs
July 22, 2022 4:52 PM–After Google Latitude was discontinued in 2013, I pulled the script from archweaver.com, but I'm leaving this article up as an artifact.  

Post a Comment

<< Home

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

Pumpkin Pie Mea

Eddys Found In the Space-Time Continuum

rad50.pl - interpret quadragesimal numbers

Out Of Kindness, Play Dumb

Oklahoma law banning Islamic law could also ban Bi...

About Roundup: Part 1, Preface and Introduction

NPR’s firing of Juan Williams an example of an eth...

Juan Williams firing was justified

What we honestly ask Google about one another

Question, Don’t Attack, Objections

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