Skip to content

Instantly share code, notes, and snippets.

@mudpuddle
Last active April 6, 2018 21:37
Show Gist options
  • Save mudpuddle/d28dcc371c05b649cafc50635f60788c to your computer and use it in GitHub Desktop.
Save mudpuddle/d28dcc371c05b649cafc50635f60788c to your computer and use it in GitHub Desktop.
Get Browser Location
function doGeolocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(positionSuccess, positionError);
} else {
positionError(-1);
}
}
function positionError(err) {
var msg;
switch(err.code) {
case err.UNKNOWN_ERROR:
msg = "Unable to find your location";
break;
case err.PERMISSION_DENINED:
msg = "Permission denied in finding your location";
break;
case err.POSITION_UNAVAILABLE:
msg = "Your location is currently unknown";
break;
case err.BREAK:
msg = "Attempt to find location took too long";
break;
default:
msg = "Location detection not supported in browser";
}
document.getElementById('info').innerHTML = msg;
setTimeout(function () {
$('#info').stop().fadeTo("slow", 0.001);
}, 5000);
}
function positionSuccess(position) {
// Center the map on the new location
var coords = position.coords || position.coordinate || position;
myLatLng = [coords.longitude, coords.latitude];
map.setCenter(myLatLng);
zoom = 12;
map.setZoom(zoom);
document.getElementById('info').innerHTML = 'Location successfully detected';
setTimeout(function () {
$('#info').stop().fadeTo("slow", 0.001);
}, 5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment