H5新特性之geolocation

geolocation是H5新增的对象,它用于定位,继承在navigator对象内,以前用navigator只用到userAgent,现在就多了这个geolocation

有2种方法(getCurrentPosition、watchPostion),4个配置属性(enableHighAccuracy,timeout,maximumAge,frequency)

getCurrentPosition:

 1 //获取定位(一次)
 2 navigator.geolocation.getCurrentPosition(
 3     data=>{
 4         // 信息都包含在data.coords里面
 5     },
 6     err=>{
 7         // err是形如 {code: 3, message: "Timeout expired"} 的对象
 8     },
 9     {
10         enableHighAccuracy:true, //高精度
11         timeout: 5000,  //超时时间
12         maximumAge: 10000 //位置缓存时间
13     }
14 )

data.coords的属性:

  • coords.latitude 纬度
  • coords.longitude 经度
  • coords.altitude 海拔
  • coords.speed 速度
  • coords.accuracy 经纬度精度
  • coords.altitudeAccuracy 海拔精度
  • coords.heading 方向,从正北开始以度计

watchPostion:

 //获取定位(一次)
 navigator.geolocation.watchPosition(
     data=>{
         // 信息都包含在data.coords里面
     },
     err=>{
         // err是形如 {code: 3, message: "Timeout expired"} 的对象
     },
     {
         enableHighAccuracy:true, //高精度
         timeout: 5000,  //超时时间
         maximumAge: 10000, //位置缓存时间
         frequency: 1000 //多久监测一次
     }
 )

ps:geolocation已经不能在http下使用了,只能在https下才行

 

转载于:https://www.cnblogs.com/amiezhang/p/7874503.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值