依赖
npm
“@amap/amap-jsapi-loader”: “^1.0.1”,
“@amap/amap-vue”: “^1.4.0”,
实现
这个如果不用显示地图也需要设置,放在生命周期前
AMapLoader.load({
key: "自己的key", // 申请好的Web端开发者Key,首次调用 load 时必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: [], //插件列表
})
.then((AMap) => {
console.log(AMap);
window.AMap = AMap;
// that.map = new AMap.Map("mymap", {
// center: [115.84414205551147, 28.678983439095823],
// zoom: 14,
// });
// this.map.on('touchmove', (e) => {
// console.log(e)
// })
})
.catch((e) => {
console.log(e);
});
这个房 public 的index.html中
<script type="text/javascript">
window._AMapSecurityConfig = {
securityJsCode: "你的安全码", //这个是web(js api)这个项目中的-高德api
}
</script>
使用
window.AMap.plugin('AMap.Geolocation', function () {
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true, //是否使用高精度定位,默认:true
// timeout: 10000, //超过10秒后停止定位,默认:5s
// buttonPosition: 'RB', //定位按钮的停靠位置
// buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
// zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
});
// map.addControl(geolocation);
geolocation.getCurrentPosition(function (status, result) {
if (status == 'complete') {
onComplete(result)
} else {
onError(result)
}
});
});
//解析定位结果
function onComplete(data) {
// let lngLat = [data.position.lng, data.position.lat]
// console.log(position);
// times(startTime);
var lat = data.position.lat;
var lon = data.position.lng;
// console.log("成功返回经纬度信息");
// Message({
// showClose: true,
// message: "成功返回经纬度信息(1)" +
// lon +
// "_" +
// lat +
// "--反应时间:" +
// times(startTime),
// type: "success",
// duration: 2000,
// });
console.log(lon, lat, "成功");
outData([lon, lat]);
}
//解析定位错误信息
function onError(data) {
// console.log(data, "see")
Message({
showClose: true,
message: "未开启位置权限",
type: "error",
duration: 2000,
});
}