代码:
initMap(callback) {
let _this = this;
let address;
window.fxtpInitMap = async () => {
let BMap = window.BMap;
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (res) {
//可以获取不同级别的位置信息,这里使用城市作为实例
console.log("获取不同级别的位置信息", res);
if (res.accuracy == null) {
// _this.$toast("请先打开获取位置权限");
} else {
const { province, city, district, street, street_number, } = res.address;
address = `${province}${
province === city ? "" : city
}${district}${street}${street_number ? street_number + "号" : ""}`;
}
callback(address);
});
};
let baiduScript = document.querySelector(
"script[data-baidu='baiduScript']"
);
var script = document.createElement("script");
script.type = "text/javascript";
script.setAttribute("data-baidu", "baiduScript");
script.src =
"xxx";
document.head.appendChild(script); //插入此标签后 会在window上挂一BMap属性,此为跨域获取的数据
},
请参考下此文档,赋予前端页面权限:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-geolocation-permission-V5