import * as KMap from "@/utils/ol-map/KMap"; import * as util from "@/common/ol_common.js"; import VectorLayer from "ol/layer/Vector.js"; import ScaleLine from "ol/control/ScaleLine"; import Overlay from "ol/Overlay"; import eventBus from "@/api/eventBus"; import Style from "ol/style/Style"; /** * @description 地图层对象 */ class HomeMap { constructor() { let that = this; let vectorStyle = new KMap.VectorStyle(); this.vectorStyle = vectorStyle; that.address = ""; } initMap(location, target) { let level = 9; let coordinate = util.wktCastGeom(location).getFirstCoordinate(); this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 7, 22); // 开启地图点击事件监听,使点位点击可以触发弹窗 this.addMapListen(); // 添加比例尺控件 const scaleLine = new ScaleLine({ units: 'metric' // 可以选择 'imperial' 或 'metric' }); this.kmap.addControl(scaleLine); } addMapListen() { let that = this // 监听地图点击事件 // 创建弹窗图层 this.popup = new Overlay({ element: document.getElementById('popup'), positioning: 'right-center', offset: [10, 10], }); this.kmap.map.addOverlay(this.popup); // 点击地图弹窗的关闭-销毁dom eventBus.on("map:destroyPopup", () => { that.popup.setPosition(undefined) }) that.kmap.on('singleclick', function (event) { let hasFeature = false let feature = that.kmap.map.forEachFeatureAtPixel(event.pixel, function (feature, layer) { if (layer instanceof VectorLayer && layer.get("name") === "MockFarmLayer") { hasFeature = true let fs = feature.get("features"); fs.length > 0 && eventBus.emit('MockFarmLayer:click', { sampleIdVal: fs[0].get("targetSampleId"), farmIdVal: fs[0].get("mockFarmId") }); } if (layer instanceof VectorLayer && layer.get("name") === "pointLayer") { hasFeature = true console.log('feature', feature) eventBus.emit("chat:showTrackDialog", feature.get("url")) } return feature; }); if (!hasFeature) { const currentLonLat = event.coordinate const params = { point: `POINT(${currentLonLat[0]} ${currentLonLat[1]})` } // VE_API.mini_farm.weather_warning_land_check(params).then(async (res) => { // await that.getLocation(currentLonLat) // if (res.code == 0) { // // 在这里可以获取feature的属性,并显示在弹窗中 // let content = '