创建marker封装
export default {
data(){
markerList:[],//marker数组 方便后续循环等操作
},
methods:{
/* 初始化marker */
initMarker(){
for (let i = 0; i < data.length; i++) {
let marker = this.createlcMaker(
data[i],
this.handlerClick.bind(this,data[i]),
this.labelHover.bind(this,data[i]),
this.labelMouseover.bind(this,data[i])
)
this.markerList.push(marker)
}
},
/* 封装创建marker */
createMarker(data,clickHandler,hoverHandler, mouseoutHandler){
var content = `<div class="way_point"></div>`;
var point = new AMap.LngLat(data.longitude, data.latitude);
var myIcon = new AMap.Icon({
image:"images/monitor/static/img/way_point.png",
size: new AMap.Size(64, 64),// 图标尺寸
});
var marker = new AMap.Marker({
position:point,
map: this.map,
content:content,
offset: new AMap.Pixel(-17, -42),// 相对于基点的偏移位置
extData:{data:data,type:'lcd'},//自定义数据
});
marker["layerType"] = '路测点';
if(clickHandler){
marker.on('click', clickHandler);
}
if(hoverHandler){
marker.on('mouseover', hoverHandler);
}
if (mouseoutHandler) {
marker.on('mouseout', mouseoutHandler);
}
// this.map.add(marker); //marker中map字段写了可以忽略此处
return marker;
},
}
}
/* 点击事件 */
handlerClick(data){
this.map.setCenter([data.longitude, data.latitude]);
var allOverlay = this.map.getAllOverlays();
for (let i = 0; i < allOverlay.length; i++) {
if (allOverlay[i]["layerType"] == '路测点') {
allOverlay[i].hide();
setTimeout(()=>{
allOverlay[i].show();
},2000)
}
}
},
/* 鼠标悬浮创建内容提示框 */
labelHover(data,e){
let content = `<div> data.rspName</div>`;
this.currentTip = new AMap.Marker({
map: this.map,
content: content, // 自定义点标记覆盖物内容
position: [Number(data.longitude), Number(data.latitude)], // 基点位置
offset: new AMap.Pixel(80, 0),// 相对于基点的偏移位置
anchor: 'center',
});
this.currentTip.setMap(this.map);
},
//鼠标移出删除内容提示框
labelMouseover(data){
if (this.currentTip) {
//this.currentTip.hide();
this.map.remove(this.currentTip)
}
this.currentTip = null;
},