记录一次h5定位

起初用了高德的精准定位,发现有概率会定位失败;于是乎在失败的回调中加了个百度的城市级定位。反馈上来的问题是定位时间太久了。
因需求只需要定位到城市级别,后又改成单独的高德地图城市定位,发现在流量的情况下,定位是不准的。
(知道怎么解决的xd麻烦留个言)
后面改用了百度的SDK赋值定位。
关键代码:

<script type="text/javascript"
    src="https://api.map.baidu.com/api?v=3.0&type=webgl&ak=be5XXd8KSpTLNuBhh8Vtad7FhqEeuqZ6"></script>
// 百度地图获取地区定位
    getCurrentPlace() {
      let that = this;

      this.$toast.loading({
        message: "定位中...",
        forbidClick: true,
        duration: 0,
      });

        var geolocation = new BMapGL.Geolocation();
        // 开启SDK辅助定位
        geolocation.enableSDKLocation();
        geolocation.getCurrentPosition(function (res) {
          // console.log("百度地图获取定位", res.address);
          that.$toast.clear();
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            console.log(res);
            console.log("百度地图获取当前位置", res.address.city);
            let cityinfo = res.address.city;
            if (cityinfo !== "深圳市") {
              that.isPlaceDisabled = false;
              return that.$toast("该活动仅限定位深圳客户参加!");
            } else {
              that.isPlaceDisabled = true;
              // 设置标识,本次浏览不再校验位置
              window.sessionStorage.setItem("newAdopt", "true");
            }
          } else {
            console.log("failed" + this.getStatus());
            that.$toast.clear();
            console.log("百度地图定位失败");
            that.$toast.fail("定位失败,请重新进入!");
            that.isPlaceDisabled = false;
          }
        });
    },

高德定位的控制台数据统计挺清晰的;高德定位官方给我有95%成功率,百度地图99.5%。目前定位还没见过失败的,时间也挺快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值