起初用了高德的精准定位,发现有概率会定位失败;于是乎在失败的回调中加了个百度的城市级定位。反馈上来的问题是定位时间太久了。
因需求只需要定位到城市级别,后又改成单独的高德地图城市定位,发现在流量的情况下,定位是不准的。
(知道怎么解决的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%。目前定位还没见过失败的,时间也挺快。