Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save isauravmanitripathi/9e7f8f578a9c72d82e26b7dccf961ce5 to your computer and use it in GitHub Desktop.
Save isauravmanitripathi/9e7f8f578a9c72d82e26b7dccf961ce5 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);
}
});
function geo_success(postion) {
printAddress(position.coords.latitude, position.coords.longitude);
}
function geo_error(err) {
//instead of displaying an error, fall back to MaxMind IP to location library
printAddress(geoip_latitude(), geoip_longitude(), true);
}
// use Google Maps API to reverse geocode our location
function printAddress(latitude, longitude, isMaxMind) {
//set yo the geocoder object
var geocoder = new google.maps.Geocoder();
//turn coordinates into an object
var yourLocation = new google.maps.LatLang(longitude, latitude);
// find out info about our location
geocoder.geocode({'latlang': yourLocation}, function (result, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (result[0]) {
$('body').append('<p>Your Address:<br/>' +
result[0].formatted_address + '</p>');
} else {
error ('Google did not return any results.');
}
} else {
error ('Reverse geocodign failed due to' + status);
}
});
// 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 error(msg) {
alert(msg);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment