Skip to content

Instantly share code, notes, and snippets.

@nikolaygit
Created November 18, 2013 12:32
Show Gist options
  • Save nikolaygit/7527079 to your computer and use it in GitHub Desktop.
Save nikolaygit/7527079 to your computer and use it in GitHub Desktop.
geolocation.js - get latitude and longitude
/* Modernizr 2.6.3 (Custom Build) | MIT & BSD
* Build: http://modernizr.com/download/#-geolocation
*/
;window.Modernizr=function(a,b,c){function t(a){i.cssText=a}function u(a,b){return t(prefixes.join(a+";")+(b||""))}function v(a,b){return typeof a===b}function w(a,b){return!!~(""+a).indexOf(b)}function x(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:v(f,"function")?f.bind(d||b):f}return!1}var d="2.6.3",e={},f=b.documentElement,g="modernizr",h=b.createElement(g),i=h.style,j,k={}.toString,l={},m={},n={},o=[],p=o.slice,q,r={}.hasOwnProperty,s;!v(r,"undefined")&&!v(r.call,"undefined")?s=function(a,b){return r.call(a,b)}:s=function(a,b){return b in a&&v(a.constructor.prototype[b],"undefined")},Function.prototype.bind||(Function.prototype.bind=function(b){var c=this;if(typeof c!="function")throw new TypeError;var d=p.call(arguments,1),e=function(){if(this instanceof e){var a=function(){};a.prototype=c.prototype;var f=new a,g=c.apply(f,d.concat(p.call(arguments)));return Object(g)===g?g:f}return c.apply(b,d.concat(p.call(arguments)))};return e}),l.geolocation=function(){return"geolocation"in navigator};for(var y in l)s(l,y)&&(q=y.toLowerCase(),e[q]=l[y](),o.push((e[q]?"":"no-")+q));return e.addTest=function(a,b){if(typeof a=="object")for(var d in a)s(a,d)&&e.addTest(d,a[d]);else{a=a.toLowerCase();if(e[a]!==c)return e;b=typeof b=="function"?b():b,typeof enableClasses!="undefined"&&enableClasses&&(f.className+=" "+(b?"":"no-")+a),e[a]=b}return e},t(""),h=j=null,e._version=d,e}(this,this.document);
function get_location() {
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
} else {
document.getElementById('geo').innerHTML =
'Your browser does not support geolocation. Upgrade it or use another one.';
}
}
function geoSuccess(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log('la = ' + latitude + ', lo=' + longitude);
}
function geoError(err) {
if (err.code == 1) {
// user said no!
console.log('User declined to share the geo location.');
} else {
console.log('Error retrieving the geo location.');
}
}
get_location();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment