<!-- ------------------ Created By InCoder ------------------ -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Detect User Location Using Javascript | InCoder</title>
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
</head>
<body>
<div class="card">
<div class="icon">
<i class="fa-solid fa-location-dot"></i>
</div>
<p></p>
<div class="location"></div>
<button class="detectBtn">Detect My Location</button>
</div>
<script src="script.js"></script>
</body>
</html>
/* ------------------ Created By InCoder ------------------ */
@import url('https://fonts.googleapis.com/css2?family=Ubuntu:ital,wght@0,300;0,400;0,500;0,700;1,300;1,400;1,500;1,700&display=swap');
* {
margin: 0;
padding: 0;
}
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background-color: #54a6d8;
}
.card {
width: 22rem;
margin: 1rem;
text-align: center;
border-radius: 10px;
background-color: #fff;
font-family: 'Ubuntu', sans-serif;
}
.icon {
font-size: 6rem;
margin-top: 1rem;
color: #424040f0;
margin-bottom: 1rem;
}
.location {
font-size: 2rem;
font-weight: bold;
color: #424040f0;
margin-bottom: 1rem;
}
.card p {
font-size: 1rem;
color: #424040f0;
margin-bottom: 1rem;
}
.detectBtn {
width: 15rem;
border: none;
color: #fff;
outline: none;
height: 2.5rem;
font-size: 1rem;
cursor: pointer;
margin-bottom: 1rem;
border-radius: .3rem;
background-color: #54a6d8;
transition: background-color .3s;
}
.detectBtn:hover {
background-color: #424040f0;
}
let text = document.querySelector('.card p');
let locationBox = document.querySelector('.location');
let detectBtn = document.querySelector('.detectBtn');
let successFunction = (position) => {
text.innerHTML = '';
detectBtn.innerHTML = 'Detecting Your Location...';
let { latitude, longitude } = position.coords;
fetch(`https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=6f6d6d4d05c04f798cdc8c808d899d9f`)
.then(response => response.json()).then(response => {
let allDetails = response.results[0].components;
console.table(allDetails);
let { county, postcode, country, state_code } = allDetails;
locationBox.innerText = `${county} ${postcode} ${state_code}, ${country}`;
detectBtn.style.display = 'none';
}).catch(() => {
detectBtn.innerText = "Something went wrong";
});
}
let errorFunction = (error) => {
if (error.code == 1) {
text.innerHTML = 'You denied to access your location';
} else if (error.code == 2) {
text.innerHTML = 'Location is not available';
} else {
text.innerHTML = 'Something went wrong';
}
}
detectBtn.addEventListener('click', () => {
if (navigator.geolocation) {
text.innerHTML = 'Allow location access to Detect your location.';
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} else {
alert('It seems like Geolocation, which is required for this page, is not enabled in your browser.');
}
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.