DEV Community

pashaliski
pashaliski

Posted on

9 5

How to use HTML5 Geolocation API

Geolocation API is used to retrieve the user's location. Since this can compromise privacy, the user first needs to click 'Allow' on the particular application to use their location.

Browser Support

The HTML5 Geolocation API is supported by most of today's moderns browsers. As of Chrome 50, the application needs to be hosted through HTTPS, if the URL is not secured the Geolocation API will not work.
g1

How to use

Here is the list of what you need to do to use this API:
-Check if Geolocation API is supported in your browser
-If supported then run getCurrentPosition() method, if not show the message
-If the getCurrentPosition() is successful, it returns a coordinate object
-The showPosition() function outputs the Latitude and Longitude and calls showMap() with the parameters.

g2

In the showMap() method we call function to show users position on the map, in our case we are using OpenStreetMap to make application what county am i in

g3

You can use Google Maps instead. This is easy to use API to show the user's position, however, you can use some Back-End library if you run any Back-End code.

Check our tutorial on how to print HTML to PDF.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️