Last active
October 10, 2015 16:37
-
-
Save yckart/3719451 to your computer and use it in GitHub Desktop.
Geolocation fallback using jQuery.ajax
This file contains hidden or 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
| /*! | |
| * 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; | |
| } | |
| }; | |
| }()); |
This file contains hidden or 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
| (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}}}()); |
Author
yckart
commented
Jun 30, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment