Skip to content

Instantly share code, notes, and snippets.

@bballentine
Created April 8, 2013 17:23
Show Gist options
  • Save bballentine/5338607 to your computer and use it in GitHub Desktop.
Save bballentine/5338607 to your computer and use it in GitHub Desktop.
WEB 2710 Geolocation Example
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script>
$(function() {
// code to run when the document is ready
if (Modernizr.geolocation){
$("p.check4location").html("Congratulations, your browser supports Geolocation!").addClass("locationSuccess");
navigator.geolocation.getCurrentPosition(
successCallback,
errorCallback_highAccuracy,
{maximumAge:600000, timeout:5000, enableHighAccuracy: true}
);
}
else {
alert("Your browser doesn't support location functionality");
$("p.check4location").html("Sorry, your browser does not support geolocation").addClass("locationFailure");
}
});
function errorCallback_highAccuracy(position) {
if (error.code == error.TIMEOUT) {
navigator.geolocation.getCurrentPosition(
successCallback,
errorCallback_lowAccuracy,
{maximumAge:600000, timeout:10000, enableHighAccuracy: false});
return;
};
}
function handle_errors(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED: alert("user did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE: alert("could not detect current position");
break;
case error.TIMEOUT: alert("retrieving position timed out");
break;
default: alert("unknown error");
break;
}
}
function successCallback(position){
var lat_message = position.coords.latitude;
var long_message = position.coords.longitude;
$("span.lat").html(" " + lat_message);
$("span.long").html(" " + long_message);
$("a.google").attr("href", "https://docs.google.com/spreadsheet/viewform?formkey=dG82ZG8xbll6REpjc0ZZMEYwNVhDbnc6MQ&entry_3=" + lat_message + "&entry_4=" + long_message);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment