Skip to content

Instantly share code, notes, and snippets.

@dkruchok
Created July 3, 2015 06:13
Show Gist options
  • Save dkruchok/9a4480bdb85603d2461c to your computer and use it in GitHub Desktop.
Save dkruchok/9a4480bdb85603d2461c to your computer and use it in GitHub Desktop.
GeoLocation file
$(document).ready(function() {
$("#findme").on("click",function(e) {
e.preventDefault();
if ( !navigator.geolocation ) {
$("#whereami").val("Your browser is also lost, probably because it doesn't support geo-location.");
} else {
$("#findme").animate({
opacity: "0.25"
},"fast");
navigator.geolocation.getCurrentPosition(positionDisplay,showGeoError);
}
});
});
function showGeoError(err) {
var reason = "Your browser lost, probably because ";
switch(err.code) {
case err.TIMEOUT: reason += "it couldn't open the map book in time."; break;
case err.PERMISSION_DENIED: reason += "it wasn't allowed to open its map book."; break;
case err.POSITION_UNAVAILABLE: reason += "the map book doesn't have an index."; break;
case err.UNKNOWN_ERROR: reason += "its in the middle of nowhere"; break;
}
$("#whereami").val(reason);
}
function positionDisplay(pos) {
$("#findme").animate({
opacity: "1"
},"slow");
$.ajax({
url: "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&latlng="+pos.coords.latitude+","+pos.coords.longitude,
dataType: "json",
success: function(data) {
var comma = /, /g;
var newline = ",\n";
$("#whereami").val(data.results[0].formatted_address.replace( comma , newline )).focus();
store("whereami",$("#whereami").val());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment