h5定位总结(h5api + 高德 + 腾讯)

H5 API定位

总结:定位非常不准,并且成功率很低。

高德定位

需要在https域名下调用,本地调用偶尔能成功

/**
 * 高德精确定位
 * @param {*} config
 * https://lbs.amap.com/api/javascript-api/reference/location#m_AMap.Geolocation
 */
export function locationGaodeExact(config = {
   }) {
   
  return new Promise((res, rej) => {
   
    AMap.plugin('AMap.Geolocation', function() {
   
      var geolocation = new AMap.Geolocation({
   
        enableHighAccuracy: true, //是否使用高精度定位,默认:true
        timeout: 1000, //超过10秒后停止定位,默认:5s
        buttonPosition: 'RB', //定位按钮的停靠位置
        buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
        zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
        ...config
      });
      geolocation.getCurrentPosition(function(status, result) {
   
        if (status == 'complete') {
   
          onComplete(result);
        } else {
   
          onError(result);
        }
      });
    });

    //解析定位结果
    function onComplete(data) {
   
      const {
   
        position: {
    lng, lat },
        formattedAddress,
        addressComponent: {
    province, city, district, adcode }
      } = data;
      const result = {
   
        longitude: lng,
        latitude: lat,
        province,
        city,
        district,
        address: formattedAddress,
        adcode // 区县code
      };
      console.log('location success', result);

      res(result);
    }
    //解析定位错误信息
    function onError(error) {
   
      console.log('location error', error);
      return rej(error);
    }

    // res({
   
    //   longitude: '120.023763,',
    //   latitude: '30.289903',
    //   city: '杭州市',
    //   district: '拱墅区',
    //   adcode: '330105'
    // })
  });
}

在PC端和IOS端经常定位失败,安卓定位还可以。

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>高德定位 和 H5定位</title>

  <style>
    * {
   
      margin: 0;
      padding: 0;
    }

    #container {
   
      width: 100%;
      height: 500px;
    }
  </style>
</head>

<body>
  <div id="container"></div>
  <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=2da0409b47ac022fb666ccaa88b4c75b"></script>
  <script>
    // h5获取 (no)
    // window.navigator.geolocation.getCurrentPosition((e) => { alert('H5-success');alert(JSON.stringify(e)) }, (e) => { alert('h5-fail') })

    // var map = new AMap.Map('container', {
   
    //   resizeEnable: true
    // })
    // var map = new AMap.Map('container', {
   
    //   // resizeEnable: true,
    //   // center: [120.023763, 30.289903],
    //   zoom: 15,
    // })

    AMap.plugin('AMap.Geolocation', function () {
   
      var geolocation = new AMap.Geolocation({
   
        // 是否使用高精度定位,默认:true
        enableHighAccuracy: true,
        // 设置定位超时时间,默认:无穷大
        timeout: 10000,
        // 定位按钮的停靠位置的偏移量,默认:Pixel(10, 20)
        buttonOffset: new 
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值