赞
踩
<template> <div> <div id="container"></div> <div ref="infoData"> <div class="info-top"> <div> <span>{{ devInfo.title }}</span> <span style="font-size: 11px; margin-left: 20px; color: #f00" >状态:{{ devInfo.state }}</span > </div> <img @click="closeInfoWindow" src="https://webapi.amap.com/images/close2.gif" /> </div> <div class="info-middle" style="background-color: white"> <img class="info-img" src="img/dev.png" /><a class="info-a-title" href="https://ditu.amap.com/detail/B000A8URXB?citycode=110105" >XXXXXXXXXXXX</a ><br />地址:{{ devInfo.address }}<br /> <div class="info-div">综合能耗:{{ devInfo.electricity }}</div> <span class="info-span"> 电{{ devInfo.gas }}<br /> <div class="info-div">水{{ devInfo.guan }}</div> <span class="info-span"> 焦炉煤气{{ devInfo.water }}</span ><br /> <div class="info-div">高炉煤气:{{ devInfo.years }}</div> </span> </div> <div class="info-bottom" style="position: relative; top: 0px; margin: 0px auto" > <img src="https://webapi.amap.com/images/sharp.png" /> </div> </div> </div> </template> <script> import AMapLoader from "@amap/amap-jsapi-loader"; export default { name: "MapComponent", data() { return { devInfo: { title: "1", icon: "1", state: "1", address: "1", electricity: "1", gas: "1", guan: "1", water: "1", years: "1", time: "1", mianji: "1", }, map: null, //初始化 map 对象 markers: [], // 存储标点 infoWindowData: [ //数据可自行修改 { title: "1号厂区", icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", //点标记图片路径 state: "正常使用", address: "XXXXXXXXXXX", electricity: "5428542°", gas: "454575NM", guan: "210个", water: "3235T", years: "10年", position: [108.971642, 34.247119], lng: 116.39, lat: 39.92, }, { title: "2号厂区", icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", //点标记图片路径 state: "已废弃", address: "XXXXXXXXXXXXX", electricity: "5428542°", gas: "454575NM", guan: "210个", water: "3235T", years: "15年", position: [108.9768933198242, 34.24537248278517], lng: 116.42, lat: 39.92, }, { title: "3号厂区", icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", //点标记图片路径 state: "正常使用", address: "XXXXXXXXXXXX", electricity: "5428542°", gas: "454575NM", guan: "210个", water: "3235T", years: "10年", position: [108.979965, 34.250659], lng: 116.41, lat: 39.91, }, ], }; }, mounted() { this.initMap(); }, methods: { initMap() { window._AMapSecurityConfig = { securityJsCode: "", // 高德地图申请的秘钥 }; AMapLoader.load({ key: "", // 申请好的Web端开发者Key,首次调用 load 时必填 version: "2.0", plugins: [ "AMap.Geolocation", "AMap.PlaceSearch", "AMap.Scale", "AMap.OverView", "AMap.ToolBar", "AMap.MapType", "AMap.PolyEditor", "AMap.CircleEditor", ], }) .then((AMap) => { const map = new AMap.Map("container", { zoom: 11, center: [116.397428, 39.90923], // 设置中心点坐标 mapStyle: "amap://styles/951766ea7040afc523d1b3bdbe33a894", //自定义样式id }); this.map = map; // 创建点标点 this.createMarkers(); }) .catch((e) => { console.log(e); }); }, // 1. 创建点标点 createMarkers() { const markers = []; let arr = []; for (let i = 0; i < this.infoWindowData.length; i++) { const positions = [ this.infoWindowData[i].lng, this.infoWindowData[i].lat, ]; const icon = new AMap.Icon({ size: new AMap.Size(20, 30), // 设置图标的宽度和高度 imageSize: new AMap.Size(20, 30), // 设置图标显示时的大小 image: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", // 自定义图标的 URL }); const marker = new AMap.Marker({ position: positions, map: this.map, icon: icon, }); markers.push( Object.assign(marker, { mapId: marker._amap_id, }) ); arr.push( Object.assign(this.infoWindowData[i], { mapId: marker._amap_id, }) ); marker.on("click", () => { this.markerClick(arr, marker); // 调用处理点击事件的方法 }); } this.markers = markers; }, //2.点击标记 获取所点击标记的信息以及窗体要展示的数据,创建信息窗体 markerClick(arr, marker) { let arrNew = arr.filter((x) => x.mapId == marker._amap_id); this.devInfo = arrNew && arrNew[0]; let infoWindow = this.createInfoWindow(); this.openInfoWindow(infoWindow, marker); }, //3.构建自定义窗体 createInfoWindow() { let infoWindowData = new AMap.InfoWindow({ isCustom: true, //使用自定义窗体 content: this.$refs.infoData, // content: this.getContent(), offset: new AMap.Pixel(16, -45), }); return infoWindowData; }, //4.打开窗体 openInfoWindow(infoWindow, marker) { infoWindow.open(this.map, marker.getPosition()); }, //5.关闭窗体 closeInfoWindow() { this.map.clearInfoWindow(); }, }, }; </script> <style> html, body, #container { height: 100%; width: 100%; } .content-window-card { position: relative; box-shadow: none; bottom: 0; left: 0; /* width: auto; */ width: 100px; padding: 0; } .content-window-card p { height: 50px; } .custom-info { border: solid 1px silver; } div.info-top { position: relative; background: none repeat scroll 0 0 #f9f9f9; border-bottom: 1px solid #ccc; border-radius: 5px 5px 0 0; } div.info-top div { display: inline-block; color: #333333; font-size: 14px; font-weight: bold; line-height: 31px; padding: 0 10px; } div.info-top img { position: absolute; top: 10px; right: 10px; transition-duration: 0.25s; } div.info-top img:hover { box-shadow: 0px 0px 5px #000; } div.info-middle { font-size: 12px; padding: 10px 6px; line-height: 20px; } div.info-bottom { height: 0px; width: 100%; clear: both; text-align: center; } div.info-bottom img { position: relative; z-index: 104; } /* span { margin-left: 5px; font-size: 11px; } */ .info-middle img { float: left; margin-right: 6px; } .info-span { /* margin-left: 35px; */ font-size: 11px; } .info-div { width: 140px; display: inline-block; margin-left: 10px; } .info-img { width: 40px; height: 40px; } .info-a-title { /* color: #000000; */ font-size: 16px; } #container { /*因为我自己的组件是在一个套了很多层的页面上使用的,所以这里需要给地图给一个固定的高,宽是100%可省略不写,否则地图会因为它自身的定位而不显示*/ height: 820px; } #container .amap-icon img, .amap-marker-content img { width: 64px; height: 64px; } </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。