Last active
April 6, 2018 21:37
-
-
Save mudpuddle/d28dcc371c05b649cafc50635f60788c to your computer and use it in GitHub Desktop.
Get Browser Location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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