User Location Detector Using HTML, CSS & JavaScript


Hello wizards, I hope you are doing great. Today in this blog you’ll learn how to create the "User Location Detector" project using HTML, CSS & JavaScript. 

Preview 


In the above video, you’ve seen the preview of the "User Location Detector" project and I hope now you are able to create this type of project. If not, I have provided all the HTML CSS and JavaScript code below. 

User Location Detector [Source Code] 

To get the following HTML, CSS & JS code for the User Location Detector project. You need to create three files one is a HTML file, second one is a CSS file and the another one is JS file. After creating these three files then you can copy-paste the given codes on your document. 

Remember, you’ve to create a file with .html extension for HTML code, .css extension for CSS code and .js for JavaScript code.

You can also download all source code files from the given download button.

HTML
<!DOCTYPE html>
<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>User Location Detector</title>
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <div class="container">
        <img src="location-image.png" alt="location-image">
        <p class="user-location">Click the "My Location" button below to get your location</p>
        <button class="myLocationBtn">My Location</button>
    </div>
    <script src="index.js"></script>
</body>

</html>
CSS
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap");

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: yellow;
  color: white;
}

.container {
  width: 400px;
  height: 350px;
  display: grid;
  place-items: center;
  background-color: black;
  border-radius: 10px;
  padding: 0 0 10px 0;
}

.container img {
  width: 200px;
  margin-bottom: -20px;
}

.container p {
  text-align: center;
  width: 80%;
}

.container .myLocationBtn {
  width: 180px;
  height: 50px;
  font-size: 18px;
  text-transform: capitalize;
  font-weight: 600;
  background-color: red;
  border: none;
  border-radius: 10px;
  color: white;
  cursor: pointer;
}

.container .myLocationBtn:active {
  transform: scale(0.95);
}
JavaScript
let userLocationEl = document.querySelector(".user-location");
let mylocationBtnEl = document.querySelector(".myLocationBtn");

//To get geographical location of the user we use Geolocation API
mylocationBtnEl.addEventListener("click", () => {
    //If the Geolocation is supported returns your location 
    if(navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(getLocation);
    }
    // If the Geolocation is not supported by the browser
    else{
        userLocationEl.innerText = "The geolocation is not supported in this browser";
    }
});

// to get the given API link. search "Openstreetmap.org" API in google
const getLocation = async (position) => {
    let response = await fetch(
    `https://nominatim.openstreetmap.org/reverse?lat=${position.coords.latitude}&lon=${position.coords.longitude}&format=json`);

    let data = await response.json();
    console.log(data)
    userLocationEl.innerText = `${data.address.state_district}, ${data.address.state}, ${data.address.country}`;
};
You have to wait 15 seconds.

Generating Download Link...

Post a Comment

Previous Post Next Post