Question

Are there any easy ways to override the default behaviors of the geolocation api and just hard code your current location?

I think this would be useful for testing and for privacy reasons (providing fake location data)

I thought there was an add on for this but I can't seem to find one. Only option right now seems to be changing the about:config geo.wifi.url to some alternative webservice, which I consider overly complicated.

Any ideas?

Thanks

Ideal Scenario

Somebody implements an add-on where a google map appears and I can pick a new default location.

Was it helpful?

Solution

The geo.wifi.uri does not need to be a webservice. You can also set it to a local uri with file://...

The file should be a json file with content like this:

{"location": {
              "latitude": 48.777025000000002, 
              "longitude": 9.1713909999999998, 
              "accuracy": 10.0}}

Update: For Firefox 9+, the format has changed:

{
    "status": "OK",
    "accuracy": 10.0,
    "location": {"lat": 48.777, "lng": 9.171}
}

Or you can combine them to support both:

{
    "status": "OK",
    "accuracy": 10.0,
    "location": {
        "lat": 48.777,
        "lng": 9.171,
        "latitude": 48.777,
        "longitude": 9.171,
        "accuracy": 10.0
    }
}

Update: it looks like manually setting this preference is blocked by the provider assuming the Google service. See Bug 716453 Update 2: geo.wifi.uri not geo.wifi.url

OTHER TIPS

The easiest way is to navigate to about:config and then in the filter box enter geo.wifi.uri, double-click the only config row that shows up, and enter the value below after you replace xxx and yyy with the co-ordinates you get from a service like http://www.getlatlon.com/:

data:application/json,{"location":{"latitude":xxx,"longitude":yyy,"accuracy":10}}

This trick doesn't require any add-ons or local/hosted files. Make sure the value is without any spaces and is on a single line!

Geolocater is an experimental add-on that lets you edit your geolocation.

In Chrome, you can use the Manual Geolocation chrome extension. It'll let you graphically choose your manual location.

Hmmm, you can't mock navigator.geolocation directly - perhaps you can refactor your code that uses it to use another object - say customGeoLocation.

In production you can just set customGeoLocation to navigator.geolocation and in tests use a mock implementation of whatever functionality you use.

EDIT: Turns out you can replace functions on navigator.geolocation object but that is still useless if you are using a mock library (like JsMock) which needs to create a mock object. So you wouldn't be able to replace navigator.geolocation with a mock.

You can do the mocking yourself in this fashion:

var getCurrentPositionCalled = false;
navigator.geolocation.getCurrentPosition = function() { 
  getCurrentPositionCalled = true; 
};

//Your app code here
//...
assert(getCurrentPositionCalled);

I have wired up Google Latitude to Firefox geolocation. This lets you manually update your location in Google Latitude with a single mouse click, instead of looking up your latitude and longitude yourself and typing it into a local file. My article on the subject is Make Firefox Take Geolocation From Google Latitude.

The solution required a small amount of server-side coding (I provide source code in the article), but I am for the time being running this code as a free live service that you can use.

Cheers, MetaEd

I have created a simple web application that serves as a geolocation endpoint you can point the browser's geo.wifi.uri to. It is compatible with the Google API specification. To use it, just enter the following, replacing [latitude] and [longitude] with the desired values:

http://geolocation-mocker.phitherek.me/geolocate/[latitude]/[longitude]

and it should work. To check if the service is up, navigate to

http://geolocation-mocker.phitherek.me

and check if the HTML page displays correctly. You can find the source code for this simple app at

https://github.com/Phitherek/geolocation-mocker

I hope this tool will prove to be useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top