Skip to content

Instantly share code, notes, and snippets.

@pims
Forked from remy/geolocation.js
Created January 29, 2010 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pims/289667 to your computer and use it in GitHub Desktop.
Save pims/289667 to your computer and use it in GitHub Desktop.
if (!navigator.geolocation) {
navigator.geolocation = (function (window) {
function getCurrentPosition(callback) {
// NOTE: for some reason, chaging the url is *allowed* with this service. Useful, but random
// source: http://www.maxmind.com/app/javascript_city
// The source is open source, as per: http://www.maxmind.com/app/api, but doesn't discuss specific license use. Hopefully it's just free to use - yay internet!
var geourl = 'http://j.maxmind.com/app/geoip.js_' + Math.random(),
iframe = document.createElement('iframe'),
doc, win;
iframe.style.display = 'none';
window.document.body.appendChild(iframe);
doc = iframe.contentDocument || iframe.contentWindow.document;
win = iframe.contentWindow;
// once the script has loaded, it triggers an onload event
// TODO: cross browser test
iframe.onload = function () {
// assign all the captured values across to our geo object
var geo = {
coords: {
latitude: win.geoip_latitude(),
longitude: win.geoip_longitude()
// other values are supported, i.e. accuracy, speed, heading, etc
},
timestamp: (new Date()).getTime()
};
// then remove the iframe from the body to clear the memory...I hope!
window.document.body.removeChild(iframe);
callback.call(callback, geo);
};
// create a document on the fly
doc.open();
doc.write('<' + 'script src="' + geourl + '"><' + '/script>');
doc.close();
}
return {
clearWatch: function () {},
getCurrentPosition: getCurrentPosition,
// TODO shouldn't be too difficult :)
watchPosition: function () {}
};
})(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment