Skip to content

Instantly share code, notes, and snippets.

@isauravmanitripathi
Created March 24, 2019 18:33
Show Gist options
  • Save isauravmanitripathi/1a5501c1b8b92c379ae195f2da887cfa to your computer and use it in GitHub Desktop.
Save isauravmanitripathi/1a5501c1b8b92c379ae195f2da887cfa to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<script>
$(document).ready(function () {
//wire up button click
$('#go').click(function () {
// test for presence of geolocation
if (navigator && navigator.geolocation) {
// make the request for the user's position
navigator.geolocation.getCurrentPosition(geo_sucess, geo_error);
} else {
// use MaxMind IP to location API fallback
printLatLong(geoip_latitude(), geoip_longitude(), true);
}
});
});
// output lat and long
function printLatLong(latitude, longitude, isMaxMind) {
$('body').append('<p>Lat: ' + lat + '</p>');
$('body').append('<p>Long: ' + long + '</p>');
// if we use MaxMind for location, add attribution link
if (isMaxMind) {
$('body').append('<p> <a href = "http://www.maxmind.com" target = "_blank"> IP to Location Service Provided by MaxMind</p>')
}
}
function geo_error(err){
// instead of displaying an error, fall back to MaxMind IP to location library printLatLong(geoip_latitude(), geoip_longitude(), true);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment