JSFiddle - React, Tailwind, and code Playground
by chenxsan
HTML
<div id="location">here</div>
JavaScript
window.onload = getMylocation;
function getMylocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayLocation, displayError);
} else {
alert('fuck it');
}
}
function displayLocation(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var div = document.getElementById("location");
div.innerHTML = lat + "\n" + lon;
}
function displayError(error) {
var errorTypes = {
0: "Unknown error",
1: "Permission denied by user",
2: "Position is not available",
3: "Request timed out"
};
var errorMessage = errorTypes[error.code];
if (error.code === 0 || error.code === 2) {
errorMessage = errorMessage + " " + error.message;
}
var div = document.getElementById("location");
div.innerHTML = errorMessage;
}