AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website. Learn More

How to Detect User Location using Javascript


Hello friends, today in this blog, we will learn how to detect user location using javascript. In our previous blog, we saw how to create a Custom Card Slider Using HTML, CSS, Javascript, and Owl Carousel Plugin. Now it's time to create a user location detector. I've also shared many projects related to Javascript. So don't forget to check here.

In order to get the location of the user, we can use the Geolocation API of JavaScript which returns the geographical position of the user. Using this API we can get the current latitude and longitude coordinates of the user if they allow it. In this small project (How to Detect User Location using Javascript), on the webpage, there is a box that has an icon and button.

You May Like These:



When you click on that button, there will open a location prompt with allow and block options. If you block the request then the button text will change to “You denied the request”. If you allow the request then there will show “detecting your location”. After a few seconds, there is shown your current location including city, postal code, state code, and country.

In the console of a browser, you’ll get many other location details including road, municipality, continent, etc. If you’re feeling difficulty with what I’m saying then you can watch a demo video or full video tutorial of it.

Note: Do not forget to get your API key from here.

Follow these steps to get API Key:
  • Go to the provided link.
  • Create an account on the OpenCage site.
  • It'll redirect you to a page after being redirected, search for API Tab.
  • Copy the API and paste it into your Code.

How to Detect User Location using Javascript [Video Tutorial]

In this video, you have seen the location detector and I hope you have understood the basic codes behind creating this location detector. As you have seen, if the user allows location access then it detects the location else it doesn't.



If you like this program (User Location Detector) and want to get source codes. You can easily get the source codes of this program. To get the source codes you just need to scroll down. You can use this location detector on your HTML pages, websites, and projects.

How to Detect User Location using Javascript [Source Code]

Initially, the preview will not run you'll have to click on the RUN PEN button then the program will run, and then you'll be able to check the preview.

You can make changes in this after creating this program like you can change the colors and background colors. You don't need to use only this design. You can create a better version of this by modifying it.

I hope you like it. If you want to create this. You'll have to create three files HTML, CSS with the extension for example:- For HTML - .HTML and for CSS - .css and for Javascript- .js just paste the given code in the files and don't forget to link the CSS and Javascript files to the HTML file.


Hurray! now you've created a user location detector. You can download files by clicking on the download button after downloading file if you are facing any kind of problem feel free to ask me in the comment section and you can also contact me via e-mail. Thanks for giving your precious time for reading this blog. Here is the download button:-

Zip File's Password = InCoderWeb

Post a Comment

Previous Post Next Post