vue3实现高德地图
1.没有高德地图的朋友们可以去高德地图Api注册账号
2.注册之后进入高德开发平台,点击应用管理下的我的应用
3.如果以前有创建过应用就不用重新创建,如果没有,则需要新建一个应用
4.创建完毕后,可以看到我们所需要的key和密钥,在后续代码中有用到
5.在vue3项目中下载loader
npm i @amap/amap-jsapi-loader --save
6.到这里就该写代码了
首先定义容器
<template>
<div>
<van-nav-bar title="地图容器" left-text="" left-arrow @click-left="onClickLeft" />
</div>
<div class="app-container">
<div style="background-color: #ffffff;">
<div id="mapcontainer"></div>
</div>
</div>
</template>
在下面script内配置所需的key和密匙
window._AMapSecurityConfig = {
securityJsCode: 'caa87ea25d73c26ecbd5ffa08767bbd1',
}
AMapLoader.load({
key: "自己创建的key", // 创建申请好的key,必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
}).then((AMap) => {
const map = new AMap.Map("container", { //设置地图容器id
viewMode: "3D", //是否为3D地图模式
zoom: 13, //初始化地图级别
center: [115.493706, 38.880989], //初始化地图中心点位置(可以自己设置)
});
可以添加插件
// 添加插件
AMap.plugin(["AMap.ToolBar", "AMap.Scale", "AMap.HawkEye", "AMap.Geolocation", "AMap.MapType", "AMap.MouseTool"], function () {
//异步同时加载多个插件
// 添加地图插件
map.addControl(new AMap.ToolBar()); // 工具条控件;范围选择控件
map.addControl(new AMap.Scale()); // 显示当前地图中心的比例尺
map.addControl(new AMap.HawkEye()); // 显示缩略图
map.addControl(new AMap.Geolocation()); // 定位当前位置
map.addControl(new AMap.MapType()); // 实现默认图层与卫星图,实时交通图层之间切换
// 以下是鼠标工具插件
const mouseTool = new AMap.MouseTool(map);
// mouseTool.rule();// 用户手动绘制折线图,测量距离
mouseTool.measureArea(); // 测量面积
});
当地图被点击的时候
map.on('click', (e) => {
// 获取点击位置的经度和纬度
const lng = e.lnglat.lng; // 经度值
const lat = e.lnglat.lat; // 纬度值
// 将当前位置的经纬度值赋给current_position变量
current_position.value = [lng, lat];
// 将当前位置的经纬度值添加到path数组中
path.value.push([lng, lat]);
});
实例化点标记
(1)第一种方式就是封装成函数来触发
function addMarker() {
const marker = new AMap.Marker({
icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
position: current_position.value, // 这里我们通过上面的点击获取经纬度坐标,实时添加标记
// 通过设置 offset 来添加偏移量
offset: new AMap.Pixel(-30, -60),
});
marker.setMap(map);
}
(2)可以把position的经纬度值写成死的
const marker = new AMap.Marker({
icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
position: [115.493706, 38.880989],
// 通过设置 offset 来添加偏移量
offset: new AMap.Pixel(-30, -60),
});
marker.setMap(map);
最后完整代码奉上
<template>
<div>
<van-nav-bar title="地图容器" left-text="" left-arrow @click-left="onClickLeft" />
</div>
<div class="app-container">
<div style="background-color: #ffffff;">
<div id="mapcontainer"></div>
</div>
</div>
</template>
<script setup>
import AMapLoader from '@amap/amap-jsapi-loader';
//在Vue3中使用时,需要引入Vue3中的shallowRef方法(使用shallowRef进行非深度监听
import { shallowRef } from '@vue/reactivity';
import { ref } from "vue";
// const map = shallowRef(null);
const onClickLeft = () => history.back(); //返回上一个页面
const path = ref([]);
const current_position = ref([]);
function initMap() {
window._AMapSecurityConfig = {
securityJsCode: '自己创建的密匙',自己创建的密匙
}
AMapLoader.load({
key: "自己创建的key", // 自己申请的key,必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
}).then((AMap) => {
const map = new AMap.Map("mapcontainer", { //设置地图容器id
viewMode: "3D", //是否为3D地图模式
zoom: 13, //初始化地图级别
center: [115.493706, 38.880989], //初始化地图中心点位置
});
// 添加插件
AMap.plugin(["AMap.ToolBar", "AMap.Scale", "AMap.HawkEye", "AMap.Geolocation", "AMap.MapType", "AMap.MouseTool"], function () {
//异步同时加载多个插件
// 添加地图插件
map.addControl(new AMap.ToolBar()); // 工具条控件;范围选择控件
map.addControl(new AMap.Scale()); // 显示当前地图中心的比例尺
map.addControl(new AMap.HawkEye()); // 显示缩略图
map.addControl(new AMap.Geolocation()); // 定位当前位置
map.addControl(new AMap.MapType()); // 实现默认图层与卫星图,实时交通图层之间切换
// 以下是鼠标工具插件
const mouseTool = new AMap.MouseTool(map);
// mouseTool.rule();// 用户手动绘制折线图,测量距离
mouseTool.measureArea(); // 测量面积
});
// 单击
map.on('click', (e) => {
// 获取点击位置的经度和纬度
const lng = e.lnglat.lng; // 经度值
const lat = e.lnglat.lat; // 纬度值
// 将当前位置的经纬度值赋给current_position变量
current_position.value = [lng, lat];
// 将当前位置的经纬度值添加到path数组中
path.value.push([lng, lat]);
});
// 实例化点标记
// 第一种(封成函数来触发)
function addMarker() {
const marker = new AMap.Marker({
icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
position: current_position.value, // 这里我们通过上面的点击获取经纬度坐标,实时添加标记
// 通过设置 offset 来添加偏移量
offset: new AMap.Pixel(-30, -60),
});
marker.setMap(map);
}
}).catch(e => {
console.log(e);
})
}
initMap()
</script>
<style>
#mapcontainer{
width: 100%;
height: 30rem;
}
</style>
效果视频奉上
vue3高德地图实现
到这里也就结束了,记得点赞哦。