1. 注册帐号
进入高德地图jsapi(lbs.amap.com/api/jsapi-v…) 先注册好【开发者帐号】,创建应用,添加KEY。
2. jsapi结合Vue学习
.按 NPM 方式安装使用 Loader :
npm i @amap/amap-jsapi-loader --save
为什么不用vue-amap是因为 vue-amap是封装好的,车辆轨迹等功能不全,原生的api很全面 作用方法网站有详细的说明,照抄就行,注意一点:
version版本为2.0时,网页上地图会有些卡,把版本更换成1.4.15就能顺畅。
3 自定义ICON# 设置车头朝向
图标方式支持旋转 angle. 但如果加了,浮框label也会随之旋转,要用自定义点标记内容,使用css 的旋转transform
//orientation 旋转角度
const carContent = orientation => {
var markerContent = document.createElement('div')
var markerImg = document.createElement('img')
markerImg.className = 'currentMaker' //设置描点img的统一样式 高宽大小等,不需要可不写
markerImg.style.transform = `rotate(${orientation}deg)`
markerImg.src = 'https://webapi.amap.com/images/car.png' //图片url
markerContent.appendChild(markerImg)
return markerContent
}
4 如何实现车子上的浮框标签效果?
var carMarkers = []
this.carList.map(car => {
const carMarker = new AMap.Marker({
content: carContent(v.orientation), //对应3部份的自定义ICON
position: car.position
})
carMarker.on('mouseover', () => {
carMarker.setLabel({
direction: 'top',
offset: new AMap.Pixel(-40, -30), //设置文本标注偏移量
content: `<div class='mapmarker-popover' >${car.name}</div>` //设置文本标注内容
})
})
carMarker.on('mouseout', () => {
carMarker.setLabel(null)
})
carMarkers.push(carMarker)
})
this.map.add(carMarkers)
车子是marker标记点,鼠标经过标记点时,setLabel,黄色块状背景用css实现代码如下:
.mapmarker-popover {
background: #ff8c00;
border:0;
margin:0;
position: absolute; //用绝对位置,相对位置有白色的留白和蓝色的边框 上下左右-1px遮挡边框
width:80px; //要定义宽高
border-radius: 2px;
height: 30px;
line-height: 30px;
text-align: center;
left:-1px;
right:-1px;
bottom:-1px;
top:-1px;
color:#fff;
&::after{ //小箭头
content:'';
position: absolute;
top:30px;
left:35px;
width:0;
height: 0;
border: 6px solid transparent;
border-top-color: #000;
border-top-color: #ff8c00;
}
}