Skip to main content

Find Your Location using HTML, CSS & JavaScript


Find Your Location using HTML, CSS & JavaScript

It is against policy to access Geolocation in Blogger.com. However, you can still find your location by clicking on CodePen button and then clicking the Get Location button to retrieve your location data

Source Code

HTML

<div class="container">
  <h1>Find Your Location</h1>
  <p>Your location: <span id="location"></span></p>
  <button id="getLocation">Get Location</button>
</div>
CSS


body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  background-color: #f5f5f5;
}

.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 50px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  text-align: center;
}

h1 {
  font-size: 36px;
  margin-bottom: 30px;
}

p {
  font-size: 24px;
  margin: 0 auto 20px;
}

#location {
  font-weight: bold;
}

button {
  background-color: #4CAF50;
  color: #fff;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  font-size: 20px;
  cursor: pointer;
}
JavaScript


// get reference to the location span and button
const locationSpan = document.getElementById('location');
const getLocationButton = document.getElementById('getLocation');

// add click event listener to the button
getLocationButton.addEventListener('click', () => {
  // check if geolocation is supported
  if (navigator.geolocation) {
    // get current position
    navigator.geolocation.getCurrentPosition(position => {
      const { latitude, longitude } = position.coords;
      locationSpan.textContent = `Latitude: ${latitude}, Longitude: ${longitude}`;
    }, error => {
      locationSpan.textContent = `Error: ${error.message}`;
    });
  } else {
    locationSpan.textContent = 'Geolocation is not supported by this browser.';
  }
});

Step By Step Explanation of above code snippet:

These following two lines of code use the document.getElementById() method to get references to the "location" span and the "getLocation" button.

JavaScript

// get reference to the location span and button
const locationSpan = document.getElementById('location');
const getLocationButton = document.getElementById('getLocation');
            


This below mentioned code adds a click event listener to the getLocation button. When the button is clicked, it checks if navigator.geolocation property is available in the browser. If it is, it calls navigator.geolocation.getCurrentPosition() method to get the user's current position. This method takes two callback functions as parameters: one to handle the successful case, and another to handle errors.

JavaScript

  getLocationButton.addEventListener('click', () => {
  // check if geolocation is supported
  if (navigator.geolocation) {
    // get current position
    navigator.geolocation.getCurrentPosition(position => {
      const { latitude, longitude } = position.coords;
      locationSpan.textContent = `Latitude: ${latitude}, Longitude: ${longitude}`;
    }, error => {
      locationSpan.textContent = `Error: ${error.message}`;
    });
  } else {
    locationSpan.textContent = 'Geolocation is not supported by this browser.';
  }
});

            

Comments

Popular posts from this blog

How to Create a Circular Scroll Progress with HTML, CSS & JavaScript

See the Pen How to Create a Circular Scroll Progress with HTML, CSS & JavaScript by Mushtaq Ahmad ( @MushtaqPhysicist ) on CodePen . How to Create a Circular Scroll Progress with HTML, CSS & JavaScript In this guide, we will explore process of using HTML, CSS, and JavaScript to design a circular scroll progress indicator. This type of indicator displays percentage of a webpage that has been scrolled. Specifically, we will create a circular indicator and position it in bottom right corner of webpage. The indicator's fill level will correspond to amount of page that has been scrolled. HTML Copy Code <!DOCTYPE html> <html lang="en"> <head> <title>Scroll Percentage</title> <!-- Stylesheet --> <link rel="stylesheet" href="style.css"> </head> <body>...

Getting Started with ReactJS: A Beginner's Guide

Getting Started with ReactJS: A Beginner's Guide Welcome to our beginner's guide to ReactJS! This guide is designed for developers who are new to React and want to learn how to create dynamic, interactive user interfaces with this popular JavaScript library. Back to TOC Table of Contents • What is ReactJS? • Setting Up Your Development Environment • Creating Your First React Component • Rendering Components • Adding Props to Your Components • Using State to Manage Data • What is ReactJS? ReactJS is an open-source JavaScript library for building user interfaces. It was created by Facebook and is now widely used by developers all over world. React allows developers to create reusable UI components and manage state of their applications using a simple and intuitive API. • Setting Up Your Development Environment To start building applications with React, you need...