vue + 高德地图 实现多个标点和窗体,新增窗体点击事件

一、前期工作
- 注册高德地图成为开发者
- 创建一个新应用、获取秘钥
二、引入
找到public index.html文件插入js,需要地图工具类的把变量加入到plugin中
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=秘钥&&plugin=AMap.Geocoder,AMap.AutoComplete,AMap.PlaceSearch"></script>
三、初始化地图,显示多个点标记,点击出现窗体
- amap.vue
<template>
<div class="my-container">
<div id="map-demo"></div>
</div>
</template>
<script>
import createInfoWindow from "@/utils/amap";
export default {
data() {
return {
map: null,
infoWindow: null,
closeImg: require("@/assets/close.png"),
pointList: [
{
address: "郑州市收到货爽肤水1",
local: [113.651398, 34.767445],
},
{
address: "郑州市收到货爽肤水2",
local: [113.851398, 34.767445],
},
{
address: "郑州市收到货爽肤水3",
local: [113.751398, 34.667445],
},
{
address: "郑州市收到货爽肤水4",
local: [113.641398, 34.747445],
},
],
winInfo: [],
winTitle: "",
markList: [],
};
},
mounted() {
this.carGPSIP();
},
methods: {
carGPSIP() {
let self = this;
this.map = new AMap.Map("map-demo", {
resizeEnable: true,
zoom: 8, //级别
center: [113.663221, 34.7568711], //中心点坐标
viewMode: "2D", //使用3D视图
mapStyle: "amap://styles/darkblue",
}); //初始化地图
//遍历生成多个标记点
for (var i = 0, marker; i < this.pointList.length; i++) {
var marker = new AMap.Marker({
position: this.pointList[i].local, //不同标记点的经纬度
map: self.map,
});
marker.title = `<span>大厅车间</span><span>设备名称</span>`;
marker.content = JSON.stringify([
"电话:" + "123456789",
"地址:" + this.pointList[i].address,
"位置:" + this.pointList[i].local,
]);
marker.on("click", self.markerClick);
// marker.emit("click", { target: marker }); //默认初始化不出现信息窗体,打开初始化就出现信息窗体
}
//实例化信息窗体
this.infoWindow = new AMap.InfoWindow({
isCustom: true, //使用自定义窗体
content: self.winInfo,
offset: new AMap.Pixel(15, -35),
});
this.map.setFitView();
},
// 点标记点击事件
markerClick(e) {
let self = this;
this.winInfo = JSON.parse(e.target.content);
this.winTitle = e.target.title;
// 设置窗体内容
this.infoWindow.setContent(
createInfoWindow.createInfoWindow(
self.winTitle,
self.winInfo.join("<br/>"),
function () {
// 关闭窗体
self.map.clearInfoWindow();
}
)
);
// 打开窗体
self.infoWindow.open(self.map, e.target.getPosition());
},
},
};
</script>
<style scoped>
#map-demo {
width: 500px;
height: 500px;
}
</style>
- utils/amap.js 封装窗体布局
function createInfoWindow(title, content,callback) {
var info = document.createElement("div");
info.className = "custom-info input-card content-window-card";
//可以通过下面的方式修改自定义窗体的宽高
info.style.width = "400px";
// 定义顶部标题
var top = document.createElement("div");
// var titleD = document.createElement("div");
var closeX = document.createElement("img");
top.className = "info-top";
closeX.src = require("@/assets/close.png");
closeX.onclick = callback;
// top.appendChild(titleD);
top.innerHTML = title;
top.appendChild(closeX);
info.appendChild(top);
// 定义中部内容
var middle = document.createElement("div");
middle.className = "info-middle";
middle.style.backgroundColor = "white";
middle.innerHTML = content;
info.appendChild(middle);
// 定义底部内容
var bottom = document.createElement("div");
bottom.className = "info-bottom";
bottom.style.position = "relative";
bottom.style.top = "0px";
bottom.style.margin = "0 auto";
info.appendChild(bottom);
return info;
}
export default {
createInfoWindow
}
- amap.scss 窗体的样式文件
.content-window-card {
position: relative;
box-shadow: none;
bottom: 0;
left: 0;
width: auto;
padding: 0;
}
.content-window-card p {
height: 2rem;
}
.custom-info {
/* border: solid 1px silver; */
}
div.info-top {
position: relative;
background: #0f49b5;
height: 30px;
line-height: 30px;
font-size: 14px;
color: #fff;
padding: 0 10px;
}
div.info-top span:nth-child(2) {
position: absolute;
left: 50%;
transform: translateX(-50%);
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: center;
}
div.info-top img {
position: absolute;
top: 8px;
right: 10px;
transition-duration: 0.25s;
width: 15px;
}
div.info-top img:hover {
box-shadow: 0px 0px 5px #000;
}
div.info-middle {
background: rgba(0,54,98,.8)!important;
font-size: 12px;
padding: 10px 6px;
line-height: 20px;
color: #fff;
}
div.info-bottom {
height: 0px;
width: 100%;
clear: both;
text-align: center;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 15px solid transparent;
border-top: 20px solid rgba(0,54,98,.8);
}
div.info-bottom img {
position: relative;
z-index: 104;
}
.info-middle img {
float: left;
margin-right: 6px;
}
四、新增窗体点击事件
参考多个文档,窗体中的点击事件不能使用@click,需要用onclick绑定,并且该事件需在初始化时定义window事件
1.绑定事件
在content里写入点击事件
2.定义事件
上述实现了窗体中点击事件,但是存在一个问题就是οnclick='handlebutton()‘不能传值(或者可以传没找对方法),我的解决方法是用DOM获取需要的值
在需要的值外面套一层标签,添加id属性,这样就可以获取到值了
版权声明:本文为weixin_45581505原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。