Skip to content

Instantly share code, notes, and snippets.

@jokemmy
Last active September 12, 2017 09:08
Show Gist options
  • Select an option

  • Save jokemmy/df57c28ee8ac9c577c508dc3af5b2b23 to your computer and use it in GitHub Desktop.

Select an option

Save jokemmy/df57c28ee8ac9c577c508dc3af5b2b23 to your computer and use it in GitHub Desktop.
浏览器地理位置API

在使用地理位置API之前首先要检测浏览器是否支持,如下测试代码:

if (navigator.geolocation) {
  // 想干嘛就干嘛
}

当然,这个if判断也能用来进行浏览器的判断,可以区分IE678浏览器与IE9和其他现代浏览器。这在我们使用某些CSS3属性时非常有用,检测浏览器是否支持某些CSS3属性相对比较麻烦,可以折中下,即在知道浏览器对该CSS3属性的支持情况下检测浏览器,一般而言就是区分区分IE678浏览器和其他浏览器,这正好与navigator.geolocation的检测是一致的。

// 摘抄 错误码的含义
function onError(error){
  switch(error.code){
    case 1:
      alert("位置服务被拒绝");
      break;
    case 2:
      alert("暂时获取不到位置信息");
      break;
    case 3:
      alert("获取信息超时");
      break;
    case 4:
      alert("未知错误");
      break;
  }
}

浏览器地理位置geolocation-api-简介

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment