Get current Geolocation using JavaScript

Today we’ll show you how to get current geolocation using JavaScript. In this small article, we will use JavaScript’s Geolocation API to get the latitude and longitude of the visitor’s position.

Let’s create a sample example to get the geolocation using JavaScript. In this example, we will show you the location information in the browser console.

Example to get current geolocation

Here we will use the Navigator.geolocation property to get the Geolocation object. Using this property, we can get the location access of the device to read the latitude and longitude.

When executing code in the browser, it will ask permission to allow access to the location.

Output

Let’s use the above code in the sample HTML page and check the logs of the browser console.

index.html

Run the above file and check in the browser.

Output - Get current Geolocation using JavaScript - Clue Mediator
Output – Get current Geolocation using JavaScript – Clue Mediator

That’s it for today.
Thank you for reading. Happy Coding..!!

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *