Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active October 10, 2015 16:37
Show Gist options
  • Select an option

  • Save yckart/3719451 to your computer and use it in GitHub Desktop.

Select an option

Save yckart/3719451 to your computer and use it in GitHub Desktop.
Geolocation fallback using jQuery.ajax
/*!
* Geolocation HTML5 API Polyfill/Shim
*
* This polyfill fixes geolocation for miscellaneous web browsers.
* It is tested and supports IE 7+, Chrome 11+ and Firefox 3.6+
*
* @Deps: http://geoplugin.net/json.gp
*
* @author: Yannick Albert
* @url: http://www.yckart.com/
* @file: jquery.geolocation.js
* @version: 1.0 - Fr, 11. Sep 2012
* @license: MIT | http://www.opensource.org/licenses/mit-license.php
*/
;(function (cache) {
if(navigator.geolocation) return;
var callback = function (show, e) {
if(cache) return show(cache);
$.ajax({
url: '//geoplugin.net/json.gp',
dataType: 'jsonp',
jsonp: 'jsoncallback',
error: e,
success: function (data) {
cache = {};
cache.coords = {};
cache.coords.latitude = data.geoplugin_latitude;
cache.coords.longitude = data.geoplugin_longitude;
show(cache);
}
});
};
navigator.geolocation = {
getCurrentPosition: callback,
watchPosition: callback,
clearWatch: function () {
cache = undefined;
}
};
}());
(function(a){if(navigator.geolocation){return}var b=function(c,d){if(a){return c(a)}$.ajax({url:"//geoplugin.net/json.gp",dataType:"jsonp",jsonp:"jsoncallback",error:d,success:function(e){a={};a.coords={};a.coords.latitude=e.geoplugin_latitude;a.coords.longitude=e.geoplugin_longitude;c(a)}})};navigator.geolocation={getCurrentPosition:b,watchPosition:b,clearWatch:function(){a=undefined}}}());
@yckart
Copy link
Copy Markdown
Author

yckart commented Jun 30, 2013

navigator.geolocation.getCurrentPosition(function (location) {
    alert(location.coords.latitude);
    alert(location.coords.longitude);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment