GeolocationCoordinates: longitude property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2020.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The longitude read-only property of the GeolocationCoordinates interface is a number which represents the longitude of a geographical position, specified in decimal degrees. Together with a timestamp, given as Unix time in milliseconds, indicating a time of measurement, the GeolocationCoordinates object is part of the GeolocationPosition interface, which is the object type returned by Geolocation API functions that obtain and return a geographical position.

Value

The value in longitude is the geographical longitude of the location on Earth described by the Coordinates object, in decimal degrees. The value is defined by the World Geodetic System 1984 specification (WGS 84).

Note: The zero meridian (also known as the prime meridian or the reference meridian) is not precisely the same as the Greenwich meridian that most people think of. It is, instead, the IERS Reference Meridian, which is located 5.3 arcseconds (102 meters / 335 feet) east of the Greenwich meridian. This is the same standard used by the Global Positioning System (GPS).

Examples

In this simple example, we fetch the user's location and display the resulting coordinates once they're returned.

JavaScript

The JavaScript code below creates an event listener so that when the user clicks on a button, the location information is retrieved and displayed.

js
let button = document.getElementById("get-location");
let latText = document.getElementById("latitude");
let longText = document.getElementById("longitude");

button.addEventListener("click", () => {
  navigator.geolocation.getCurrentPosition((position) => {
    let lat = position.coords.latitude;
    let long = position.coords.longitude;

    latText.innerText = lat.toFixed(2);
    longText.innerText = long.toFixed(2);
  });
});

After setting up variables to more conveniently reference the button element and the two elements into which the latitude and longitude will be drawn, the event listener is established by calling addEventListener() on the <button> element. When the user clicks the button, we'll fetch and display the location information.

Upon receiving a click event, we call getCurrentPosition() to request the device's current position. This is an asynchronous request, so we provide a callback which receives as in put a GeolocationPosition object describing the determined position.

From the GeolocationPosition object, we obtain the user's latitude and longitude using position.coords.latitude and position.coords.longitude so we can update the displayed coordinates. The two <span> elements are updated to display the corresponding values after being converted to a value with two decimal places.

HTML

The HTML used to present the results looks like this:

html
<p>
  Your location is <span id="latitude">0.00</span>° latitude by
  <span id="longitude">0.00</span>° longitude.
</p>
<button id="get-location">Get My Location</button>

Result

Take this example for a test drive here:

Specifications

Specification
Geolocation
# latitude-longitude-and-accuracy-attributes

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
longitude
Chrome – Full support
Chrome 5 (Release date: 2010-05-25)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 3.5 (Release date: 2009-06-30)
footnote Full support
Opera – No support
Opera 10.6 – 12.1 (Release date: 2010-07-01)
footnote Removed in 15 and later
Opera – Full support
Opera 16 (Release date: 2013-08-27)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – No support
Opera Android 11 – 12.1 (Release date: 2011-03-22)
footnote Removed in 14 and later
Opera Android – Full support
Opera Android 16 (Release date: 2013-09-18)
footnote Full support
Safari on iOS – Full support
Safari on iOS 3 (Release date: 2009-06-17)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 3 (Release date: 2009-06-17)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

See also