首页 > 其他分享 >vue+ openlayers + GeoServer 地图初始化 标点加弹窗看详情

vue+ openlayers + GeoServer 地图初始化 标点加弹窗看详情

时间:2022-11-22 12:13:01浏览次数:55  
标签:vue const popup import GeoServer ol openlayers vehicle new

<template>
<div class="mapCont">
<div id="map">
<div id="popup" ref="popup" class="ol-popup" v-show="vehiclePointInfo">
<div id="popup-closer" class="ol-popup-closer" @click="closePopup()"></div>
<div id="popup-content"> {{vehiclePointInfo.name}}</div>
</div>
</div>
</div>
</template>

<script>
import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import TileWMS from "ol/source/TileWMS";
// import ImageWMS from "ol/source/ImageWMS";
import { fromLonLat } from 'ol/proj';
import Point from 'ol/geom/Point';
import Overlay from 'ol/Overlay'
import Feature from 'ol/Feature';
import {Icon, Style} from 'ol/style';
import {Modify} from 'ol/interaction';
import {Vector as VectorLayer} from 'ol/layer';
import { Vector as SourceVector } from 'ol/source'

export default {
data() {
return {
map: null,
target:null,
infoWindow: undefined,
sourceVector: new SourceVector({}),
markerList:[],
vehiclePointInfo:{},
listData:[
{
'id':'4545',
'name':'川A45235',
'selected':false,
'longitude':'104.065681',
'latitude':'30.653442'
},
{
'id':'42545',
'name':'川W45S44',
'selected':false,
'longitude':'104.088691',
'latitude':'30.664442'
}
]
};
},
created(){},
methods: {
init() {
const format = 'image/png';
const TileWMSLayer = new TileLayer({
source: new TileWMS({
url: "你的后台给你的地图地址",
// Layers需要指定要显示的图层名
params: {
'FORMAT': format,
'VERSION': '1.1.1',
tiled: true,
"STYLES": '',
"LAYERS": 'osm:osm',
"exceptions": 'application/vnd.ogc.se_inimage',
tilesOrigin: -20037508.342789244 + "," + -20048966.1040146
}
})
})
/* const imageLayer = new TileLayer({
source: new ImageWMS({
ratio: 1,
url: process.env.VUE_APP_MAP_BASE_URL + "/geoserver/osm/wms",
params: {
'FORMAT': format,
'VERSION': '1.1.1',
"STYLES": '',
"LAYERS": 'osm:osm',
"exceptions": 'application/vnd.ogc.se_inimage',
}
})
});*/
const diyMonitorMarkerLayer = new VectorLayer({
source: this.sourceVector
});
const target = document.getElementById('map');
this.target = target;
this.map = new Map({
layers: [TileWMSLayer,diyMonitorMarkerLayer],
target: target,
view: new View({
projection: "EPSG:4326",
center: [104.065681, 30.653442],
zoom: 12,
maxZoom: 18,
minZoom: 8
})
});
// this.focusIcon(this.sourceVector,diyMonitorMarkerLayer,target) // 鼠标移动到icon上 聚焦点展示
const popup = document.getElementById('popup')
if (!this.infoWindow) {
this.infoWindow = new Overlay({
id: 'infoWindow',
element: popup,
stopEvent: true,
insertFirst: true,
offset: [0, 0], // 气泡离icon标点的位置
autoPan: false,
className: 'overlay-popup'
})
}
},
// 初始化icon图标
createPointIcon (vehicleList) {
const markFeatures = [];
const markerList = [];
vehicleList.forEach(vehicle => {
const marker = this.createOverlay(vehicle);
const iconStyle = new Style({
image: new Icon({
src: '/img/car2.png',
scale: 0.8
})
});
// vector
const iconFeature = new Feature({
geometry: new Point([vehicle.longitude, vehicle.latitude]),
info: vehicle,
tooltip: marker
});
iconFeature.setStyle(iconStyle);
markFeatures.push(iconFeature);
markerList.push(marker)
});
this.sourceVector.addFeatures(markFeatures);
markerList.forEach(tooltip => {
this.map.addOverlay(tooltip) //*在地图上展示标签-addOverlay 全局保存到markerList 对应的-removeOverlay
});
this.markerList = markerList;
},
//点击标签 - 弹窗展示
createOverlay (vehicle) {
const element = document.createElement('div');
element.innerHTML = vehicle.name;
element.className = 'tooltip-Unline';
const that = this;
// that.map.getView().setCenter(fromLonLat([vehicle.longitude, Number(vehicle.latitude) - 0.006]))
// 更据selectedKeys是否有值,来判断是否点击tree name来自动弹框
element.addEventListener('click', function () {
// 获取当前icon弹框详情
that.vehiclePointInfo = vehicle;
that.map.removeOverlay(that.infoWindow);
that.map.addOverlay(that.infoWindow);
that.infoWindow.setPosition([vehicle.longitude, vehicle.latitude])
})
return new Overlay({
id: vehicle.id,
insertFirst: false,
element: element,
offset: [8, -12],
positioning: 'right-top',
position: [vehicle.longitude, vehicle.latitude]
})
},
// 鼠标移动到icon上 聚焦点展示
focusIcon (vectorSource,vectorLayer,target) {
const vm = this
const modify = new Modify({
hitDetection: vectorLayer,
source: vectorSource,
});
modify.on(['modifystart', 'modifyend'], function (evt) {
target.style.cursor = evt.type === 'modifystart' ? 'grabbing' : 'pointer';
});
const overlaySource = modify.getOverlay().getSource();
overlaySource.on(['addfeature', 'removefeature'], function (evt) {
target.style.cursor = evt.type === 'addfeature' ? 'pointer' : '';
});

vm.map.addInteraction(modify);
},
// 气泡关闭
closePopup () {
this.infoWindow.setPosition(undefined)
this.map.removeOverlay(this.infoWindow)
this.$emit('closePopup', this.vehiclePointInfo)
},
},
mounted() {
this.init(); // 地图加载地图
this.createPointIcon(this.listData) // icon初始化
}
};
</script>

<style lang="less" scoped>
.mapCont{
height: 100%;
width: 48vw;
#map{
height: 100%;
width: 100%;
}
}
/deep/ .tooltip-Unline {
background: #fc6a3e!important;
padding: 0 8px;
color: #000000!important;
border-radius: 2px;
cursor: pointer;
margin-left: 2px;
z-index: 9999999;
}

/deep/ .overlay-popup {
z-index: 1000;
}

.ol-popup {
position: absolute;
z-index: 1000;
color: #001656;
background-color: #e8e8f7;
opacity: 1;
-webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
padding: 15px;
border-radius: 10px;
border: 1px solid #cccccc;
bottom: 24px;
left: -50px;
min-width: 200px;
}

.ol-popup:after,
.ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}

.ol-popup:after {
border-top-color: #eeeeee;
border-width: 10px;
left: 48px;
margin-left: -10px;
}

.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}

.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 2px;
right: 8px;
}

.ol-popup-closer:after {
content: "✖";
}
</style>
<style lang="less">
.ol-zoom {
top: auto;
bottom: .5em;
}

.ol-control button {
display: inline-block !important;
}

.ol-full-screen {
top: auto;
bottom: .5em;
}

</style>

标签:vue,const,popup,import,GeoServer,ol,openlayers,vehicle,new
From: https://www.cnblogs.com/lihong-123/p/16914718.html

相关文章

  • ⑧ vue+echarts实现热词分析
    依赖"dependencies":{"echarts":"^4.0.4","echarts-wordcloud":"^1.1.3",},tip:echarts-wordcloud现在有2.0和1.x两个版本,2.0对应echarts5.x版本......
  • vue 项目中,后端返回文件流,导出excel
    之前写过文件流导出excel,这次直接把上次的代码拿过来复制粘贴,但是导出的表格里面没有数据,只显示undefined。这是之前的代码//api接口页面//excel导出接口export......
  • VUE
    VUESoc:HTML+css+js:视图网络通信:axios页面跳转:vue-router状态管理:vuexVue-UI:ICE  M:模型V:视图C:控制器View:JSP{{}}DATA:mvvm:数据双向绑定 虚拟Dom:利......
  • swiper鼠标悬停(自由切换),拒绝大佬,珍爱生命![email protected]
    [email protected]@4.1.1--save-dev实测有效!JavaScript:<script>import{Swiper,SwiperSlide}from'vue-awesome......
  • vue2 计算属性9 watch immediate
    watch:监听数据发生的变化 newVal是变化后的新值,oldVal是变化前的旧值 一般都是带着接口查询的watch:{username(newVal){if(newVal==='')return$get('https:......
  • vue2-vm.$set,vm.$delete实现(三)
    vm.$set实现语法:vm.$set(target,key,value)参数:{Object|Array}target{String|Number}key{any}value返回值:{Function}unwatch用法:在object上设置一个属......
  • vue学习笔记
     --vue笔记 --页面带参数跳转testBang(){this.$router.push({path:"/layout/channel/channelAuth",query:{startDate:"test",}})}--获取url入参lettok......
  • avue框架 拼接后端返回的数据到table中
    根据要求展示下列详细地址情况: 后端返回的数据:  具体实现步骤:  {label:"详细地址",prop:"buildingName",display:false,width:130,......
  • vue3 封装axios
    1添加一个新的http.js文件封装axios 引入axios //引入Axiosimportaxiosfrom'axios'定义一个根地址//视你自己的接口地址而定varroot2='http://121.4.6......
  • Vue3笔记 - minxin和hook的使用与对比
    minxin和hook的使用与对比目录minxin和hook的使用与对比1.mixin(Vue2)2.hook(Vue3)3.mixin和hook的对比1.mixin(Vue2)mixin可以把多个组件共用的配置提取成一......