Geolocation API

Get user location using Geolocation API

IntermediateTopic: Browser API
Back

JavaScript Geolocation API Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
        (position) => {
            console.log('Latitude:', position.coords.latitude);
            console.log('Longitude:', position.coords.longitude);
        },
        (error) => console.error('Error:', error.message)
    );
}
Output
Latitude: 40.7128
Longitude: -74.0060

Understanding Geolocation API

Geolocation API gets user location.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Table of Contents