安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

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

句法

let longitude = geolocationCoordinatesInstance.longitude
					

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

注意 : 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).

范例

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.

let button = document.getElementById("get-location");
let latText = document.getElementById("latitude");
let longText = document.getElementById("longitude");
button.addEventListener("click", function() {
  navigator.geolocation.getCurrentPosition(function(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 logitude will be drawn, the event listener is established by calling addEventListener() <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.

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:

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

结果

Take this example for a test drive here:

规范

规范 状态 注释
地理位置 API
The definition of 'Coordinates.longitude' in that specification.
推荐 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
longitude Chrome 5 Edge 12 Firefox 3.5 IE 9 Opera 16
16
不支持 10.6 — 15
Safari 5 WebView Android 37 Chrome Android 18 Firefox Android 4 Opera Android 16
16
不支持 11 — 14
Safari iOS ? Samsung Internet Android 1.0

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: