The Coordinates interface's read-only longitude property is a double-precision floating point value which represents the longitude of a geographical position, specified in decimal degrees. Together with a DOMTimeStamp indicating a time of measurement, the Coordinates object is part of the Position interface which is the object type returned by Geolocation API functions that obtain and return a geographical position.

For the safety and security of users, geolocation information is only available in secure contexts (that is, HTTPS sessions).

Syntax

longitude = coordinates.longitude

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).

It may be worth noting that the zero meridian (also known as the prime meridian or the reference meridian) is not precisely the same as the Greenwhich 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).

Example

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.

After setting up variables to more conveniently reference the button element and the two elements into which the latitude and logitude 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 Position object describing the determined position.

From the Position 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.