Get User Location using HTML, CSS & JavaScript

Get User Location using HTML, CSS & JavaScript

Get User Location using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Get User Location which will tell the exact location of the user once he click the button all he has to do is grant the permission which pop on his/her browser. This project will is good for intermediate and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Get User Location Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Get User Location Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Get User Location Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Get User Location Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Get User Location

First we’ll start with creating the structure of the Get User Location project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Get User Location</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <img src="https://i.postimg.cc/8PPRxPwc/location.png" />
      <div id="location-details">Click on the 'Get Location' Button</div>
      <button id="get-location">Get Location</button>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

CSS Code for Get User Location

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Get User Location project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(45deg, #0076ec, #42a1ff);
}
.container {
  width: 80vw;
  max-width: 37.5em;
  background-color: #ffffff;
  padding: 3em 1.8em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  border-radius: 0.6em;
  box-shadow: 0 0.6em 2.5em rgba(0, 7, 70, 0.2);
}
.container img {
  width: 6.25em;
  display: block;
  margin: auto;
}
#location-details {
  font-size: 1.75em;
  text-align: center;
  margin: 1em 0 1.7em 0;
  color: #021d38;
  font-weight: 500;
}
.container button {
  display: block;
  margin: auto;
  background-color: #42a1ff;
  color: #ffffff;
  border: none;
  font-size: 1.25em;
  padding: 1em 2.5em;
  border-radius: 0.25em;
  cursor: pointer;
}

Portfolio Website using HTML and CSS (Source Code)

JavaScript Code for Get User Location

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Get User Location using HTML, CSS & JavaScript (Source Code).

let locationButton = document.getElementById("get-location");
let locationDiv = document.getElementById("location-details");

locationButton.addEventListener("click", () => {
  //Geolocation APU is used to get geographical position of a user and is available inside the navigator object
  if (navigator.geolocation) {
    //returns position(latitude and longitude) or error
    navigator.geolocation.getCurrentPosition(showLocation, checkError);
  } else {
    //For old browser i.e IE
    locationDiv.innerText = "The browser does not support geolocation";
  }
});

//Error Checks
const checkError = (error) => {
  switch (error.code) {
    case error.PERMISSION_DENIED:
      locationDiv.innerText = "Please allow access to location";
      break;
    case error.POSITION_UNAVAILABLE:
      //usually fired for firefox
      locationDiv.innerText = "Location Information unavailable";
      break;
    case error.TIMEOUT:
      locationDiv.innerText = "The request to get user location timed out";
  }
};

const showLocation = async (position) => {
  //We user the NOminatim API for getting actual addres from latitude and longitude
  let response = await fetch(
    `https://nominatim.openstreetmap.org/reverse?lat=${position.coords.latitude}&lon=${position.coords.longitude}&format=json`
  );
  //store response object
  let data = await response.json();
  locationDiv.innerText = `${data.address.city}, ${data.address.country}`;
};

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

Final Output


We have Successfully created our Get User Location using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply