Skip to content

Instantly share code, notes, and snippets.

@kozog
Created January 29, 2014 09:38
Show Gist options
  • Save kozog/8684604 to your computer and use it in GitHub Desktop.
Save kozog/8684604 to your computer and use it in GitHub Desktop.
function Geolocator (modernizr, jquery, fallbackApi) {
this.jquery = jquery;
this.modernizr = modernizr;
this.fallbackApi = fallbackApi;
}
Geolocator.prototype.is64BitFirefox = function () {
var pattern = /.+Ubuntu.+Linux x86_64.+Firefox.+$/;
var match = navigator.userAgent.match(pattern);
console.log(this.modernizr.geolocation);
if (match != null) {
return true;
}
return false;
}
Geolocator.prototype.getLocation = function (options) {
if (!this.modernizr.geolocation || this.is64BitFirefox()) {
this.jquery.ajax({
type: "GET",
url: this.fallbackApi,
})
.done(function(data) {
var result = {
'latitude': data.latitude,
'longitude': data.longitude
};
options.success(result);
})
.fail(options.error)
;
} else {
navigator.geolocation.getCurrentPosition(
function (data) {
options.success(data.coords);
},
options.error
);
}
};
// Example use
var gloc = new Geolocator(
Modernizr,
$,
'http://freegeoip.net/json/'
);
gloc.getLocation({
success: function (point) {
// returned point will have latitude and longitude properties
console.log(point);
},
error: function (data) {
console.log(data);
alert('Error');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment