美文网首页
原生JS获取用户地理位置

原生JS获取用户地理位置

作者: 刘凯gg | 来源:发表于2019-03-18 11:56 被阅读0次

获取用户地理位置关键api:

navigator.geolocation.getCurrentPosition(showPosition,showError,option)

其中三个参数分别表示:

showPosition : 成功的回调

showError : 错误的回调

option:数据获取的方式

showPosition属性

coords.accuracy : 获取位置的精度

coords.altitude : 获取海拔(米)

coords.altitudeAccuracy : 获取位置的海拔精度

coords.heading : 获取方向

coords.latitude : 获取纬度

coords.longitude : 获取经度

coords.speed : 获取速度

timestamp : 获取相应的时间

showError 属性

PERMISSION_DENIED : 用户拒绝页面发起的地理位置

POSITION_UNAVAILAVLE : 无法获取当前位置

TIMEOUT : 超时

option 参数

enableHighAcuracy : 是否启动高精模式(布尔值)

maximumAge : 设置定位缓存过期的时间(毫秒,0为禁用缓存)

timeout : 设置获取定位信息的时常 (超时触发ErrorCallback)

代码如下:


相关文章

网友评论

      本文标题:原生JS获取用户地理位置

      本文链接:https://www.haomeiwen.com/subject/hakvmqtx.html