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下才行

 

posted @ 2017-11-21 17:47  张啊咩  阅读(896)  评论(0编辑  收藏  举报