Created
January 29, 2010 00:22
-
-
Save remy/289323 to your computer and use it in GitHub Desktop.
Adds geolocation support via JavaScript and a service from maxmind.com to add the lat/long - perfect :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!navigator.geolocation) { | |
navigator.geolocation = (function (window, document) { | |
function getCurrentPosition(callback) { | |
var geourl = 'http://www.google.com/jsapi?' + 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.google.loader.ClientLocation.latitude, | |
longitude: win.google.loader.ClientLocation.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, document); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ha! That's new!
I've been meaning to convert the code to use Google - let me see if I can't fix this :)