Enable geolocation in a xulrunner-based app
By glazou on Wednesday 22 July 2009, 12:19 - Mozilla - Permalink
As you certainly already know, Firefox 3.5 includes a geolocation provider based on Google APIs. It allows to retrieve the user's latitude and longitude, and Doug Turner recently added support for reverse geolocation. Very cool.
But to use this in recent xulrunner builds, you need a few things and geolocation will not work without them:
- you need a pref to enable geolocation
pref("geo.enabled", true);
- You need a pref to specify Google geolocation API
pref("geo.wifi.uri", "https://www.google.com/loc/json");
- You also need a JS component derived from lines 1036 and following in nsBrowserGlue.js. The minimal component you need, if you want to always allow geolocation requests without prompting the user about it, is the following one:
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cr = Components.results;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function GeolocationPrompt() {}
GeolocationPrompt.prototype = {
classDescription: "Geolocation Prompting Component",
classID: Components.ID("{C6E8C44D-9F39-4AF7-BCC0-76E38A8310F5}"),
contractID: "@mozilla.org/geolocation/prompt;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIGeolocationPrompt]),
prompt: function(request) {
request.allow();
return;
}
};
//module initialization
function NSGetModule(aCompMgr, aFileSpec) {
return XPCOMUtils.generateModule([GeolocationPrompt]);
}
If you need more than that, just tweak the prompt
method above and do a request.allow()
or request.cancel()
depending on your wishes.
All the above done, you can safely query navigator.geolocation.getCurrentPosition
.
Comments
Note that you are NOT allowed to use the "Google geolocation API" as you called it, officially Google Location Service (GLS) without an explicit permission from Google. They intend to set up a system for anyone to get that permission, but for now you need an explicit contract with them (Mozilla Corporation has one for the Firefox and Fennec products only, not even SeaMonkey is allowed to just use it).