Web Geolocation API

Kai aro ba dake kai.

HTML Geolocation API yin kai aro ba dake kai.

Kwarin kai kai ba da a fiya, yin da ba kai aro ba, dake ba da yin aro ba.

亲自试一试

注释:地理定位对于带有 GPS 的设备(如智能手机)最为准确。

所有浏览器都支持 Geolocation API:

Chrome IE Firefox Safari Opera
支持 支持 支持 支持 支持

注释:从 Chrome 50 开始,Geolocation API 将仅适用于安全上下文,例如 HTTPS。如果您的站点托管在非安全源(例如 HTTP)上,则获取用户位置的请求将不再起作用。

使用 Geolocation API

getCurrentPosition() 方法用于返回用户的位置。

下面的例子返回用户位置的纬度和经度:

实例


亲自试一试

例子解释:

  1. 检查是否支持 Geolocation
  2. 如果支持,请运行 getCurrentPosition() 方法。如果没有,则向用户显示一条消息
  3. 如果 getCurrentPosition() 方法成功,则返回一个 coordinates 对象给参数 (showPosition) 中规定的函数
  4. showPosition() 函数输出纬度和经度

上面的例子是一个非常基本的地理定位脚本,没有错误处理。

处理错误和拒绝

getCurrentPosition() 方法的第二个参数用于处理错误。如果无法获取用户的位置,它规定要运行的函数:

实例

function showError(error) {
  switch(error.code) {
    case error.PERMISSION_DENIED:
      x.innerHTML = "User denied the request for Geolocation."
      break;
    case error.POSITION_UNAVAILABLE:
      x.innerHTML = "Location information is unavailable."
      break;
    case error.TIMEOUT:
      x.innerHTML = "The request to get user location timed out."
      break;
    case error.UNKNOWN_ERROR:
      x.innerHTML = "An unknown error occurred."
      break;
  }
}

亲自试一试

在地图中显示结果

如需在地图中显示结果,您需要访问地图服务,例如 Google 地图。

In the following example, the returned latitude and longitude are used to display the location on Google Maps (using a static image):

实例

function showPosition(position) {
  let latlon = position.coords.latitude + "," + position.coords.longitude;
  let img_url = "https://maps.googleapis.com/maps/api/staticmap?center=
  "+latlon+"&zoom=14&size=400x300&sensor=false&key=YOUR_KEY";
  document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>";
}

Location-specific information

This page demonstrates how to display the user's location on a map.

Geolocation is also very useful for location-specific information, such as:

  • Latest local information
  • Display points of interest near the user
  • Turn-by-turn navigation (GPS)

getCurrentPosition() method - Return data

getCurrentPosition() The method returns an object on success. It will always return latitude, longitude, and accuracy properties. If available, it will return other properties:

Properties Return
coords.latitude Latitude expressed as a decimal number (always returned).
coords.longitude Longitude expressed as a decimal number (always returned).
coords.accuracy The accuracy of the location (always returned).
coords.altitude Average height above sea level (in meters) (if available, it will be returned).
coords.altitudeAccuracy The height accuracy of the location (if available, it will be returned).
coords.heading The heading from north clockwise (if available, it will be returned).
coords.speed Speed in meters/second (if available, it will be returned).
timestamp The response date/time (if available, it will be returned).

Geolocation object - Other interesting methods

The Geolocation object also has other interesting methods:

  • watchPosition() - Return the user's current location and continue to return updated locations as the user moves (such as GPS in a car).
  • clearWatch() - Stop the watchPosition () method.

下面的例子展示了 watchPosition() 方法。你需要准确的 GPS 设备来测试(比如智能手机):

实例


亲自试一试