HTML5地理位置定位

export function getCurrentPosition() {
  const pos = "geolocation" in navigator;
  const options = {
    enableHighAccuracy: true,
    maximumAge: 30000,
    timeout: 30000
  }
  if (pos) {
    return new Promise((resolve, reject) => {
      navigator.geolocation.getCurrentPosition(showPosition, showError, options);

      function showPosition(position) {
        resolve(position)
      }

      function showError(error) {
        switch (error.code) {
          case error.PERMISSION_DENIED:
            reject("定位失败,用户拒绝请求地理定位");
            break;
          case error.POSITION_UNAVAILABLE:
            reject("定位失败,位置信息不可用");
            break;
          case error.TIMEOUT:
            reject("定位失败,请求获取用户位置超时");
            break;
          case error.UNKNOWN_ERROR:
            reject("定位失败,定位系统失效");
            break;
          default:
            reject("定位失败");
        }
      }
    })
  } else {
    return Promise.reject("不支持地理定位");
  }
}

你可能感兴趣的:(HTML5地理位置定位)