本文实例为大家分享了vue+openlayers加载geojson并实现点击弹窗教程,供大家参考,具体内容如下
第一步:安装vue-cli
cnpm install -g @vue/cli
第二步:新建一个项目
1.新建项目 (vue-openlayers为项目名),并选择default模版
vue create vue-openlayers
2.安装openlayers
cnpm i -S ol
第三步:写业务代码
1.删除掉HelloWorld.vue 新建 olmap.vue组件
components/olmap.vue代码:
<template>
<div id="map" ref="rootmap">
<div class="vm">
<!-- <h2 class="h-title">弹窗 popup</h2> -->
<!-- 弹窗元素 -->
<div id="popup" class="ol-popup" ref="popup">
<a href="#" id="popup-close" class="ol-popup-closer" @click="closePopup"></a>
<div class="popup-content">
<table id="routeBox">
<tbody>
<tr>
</tr>
<tr>
<td>所在图层:</td>
<td>{{layerName}}</td>
</tr>
<tr>
<td>handle:</td>
<td>{{handle}}</td>
</tr>
<tr>
<td>块名称:</td>
<td>{{blockName}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</template>
<script>
import "ol/ol.css";
import { Map, View } from "ol";
// import TileLayer from "ol/layer/Tile";
import VectorLayer from "ol/layer/Vector";
// import OSM from "ol/source/OSM";
import VectorSource from "ol/source/Vector";
// import Feature from "ol/Feature";
import GeoJSON from "ol/format/GeoJSON";
import Style from "ol/style/Style";
import Stroke from "ol/style/Stroke";
import Fill from "ol/style/Fill";
// import Select from "ol/interaction/Select"
// import {bbox} from 'ol/loadingstrategy';
import Point from "ol/geom/Point";
import { transform } from "ol/proj";
import Text from "ol/style/Text";
import Overlay from "ol/Overlay";
export default {
data() {
return {
map: null,
allFeatures: null,
layerName: null,
blockName: null,
handle: null,
overlayer: null,
};
},
mounted() {
this.initMap()
},
methods: {
initMap(){
var extent = [11285.07103919199,20056.574012374178,61290.31172946711,33996.47243386325];
var wfsVectorSource = new VectorSource({
url: 'http://localhost:8082/geoserver/workhome/ows"map",
layers: [
wfsVectorLayer
],
view: new View({
center: [31955.4551374715, 28165.253430237015],
projection: 'EPSG:3857',
zoom: 14
}),
});
// this.map.addLayer()
this.map.getView().fit(extent, this.map.getSize());
// this.map.getView().setZoom(14);
var that = this
// 2. 创建Overlay图层
that.overlayer = new Overlay({
element: this.$refs.popup, // 弹窗标签,在html里
autoPan: true, // 如果弹窗在底图边缘时,底图会移动
autoPanAnimation: { // 底图移动动画
duration: 250
}
})
if(timer){
clearInterval(timer)
}
var timer = setTimeout(() =>{
var fs = wfsVectorSource.getFeatures()
that.allFeatures = fs
console.log('allFeatures',that.allFeatures)
},3000);
//Vector第一种单击事件
// var selectSingleClick = new Select();
// this.map.addInteraction(selectSingleClick);
// selectSingleClick.on('select', function(e) {
// // var p = e.mapBrowserEvent.coordinate
// // console.log('p',p)
// console.log(e)
// var features=e.target.getFeatures().getArray();
// if (features.length>0)
// {
// console.log('length',features.length)
// var feature=features[0];
// console.log('feature',feature)
// }
// })
//Vector第二种单击事件
this.map.on('singleclick',mapClick);
function mapClick(e){
var p = e.coordinate
var p1 = new Point(transform(p, 'EPSG:3857', 'EPSG:4326')).getCoordinates();
console.log(p)
console.log('this.allFeatures.length',that.allFeatures)
for(let j=0;j<that.allFeatures.length-1;j++){
var b1 = new Point(transform(that.allFeatures[j].getGeometry().getClosestPoint(p), 'EPSG:3857', 'EPSG:4326')).getCoordinates();
var b2 = new Point(transform(that.allFeatures[j+1].getGeometry().getClosestPoint(p), 'EPSG:3857', 'EPSG:4326')).getCoordinates();
var x1 = that.getDistance(p1[0],p1[1],b1[0],b1[1]);
var x2 = that.getDistance(p1[0],p1[1],b2[0],b2[1]);
let fea = that.allFeatures[j+1]
if(x1<x2){
that.allFeatures[j+1] = that.allFeatures[j]
that.allFeatures[j] = fea
}
}
let a = that.allFeatures[that.allFeatures.length-1]
that.overlayer.setPosition(p)
that.map.addOverlay(that.overlayer)
a.setStyle(that.polygonStyle())
that.map.getView().setCenter(p)
console.log(a)
}
},
// 关闭弹窗
closePopup: function(){
console.log(this)
// 把弹窗位置设置为undefined,并清空坐标数据
this.overlayer.setPosition(undefined)
this.currentCoordinate = null
},
//计算两点之间距离
getDistance: (lat1, lng1, lat2, lng2)=>{
lat1 = lat1 || 0;
lng1 = lng1 || 0;
lat2 = lat2 || 0;
lng2 = lng2 || 0;
var rad1 = lat1 * Math.PI / 180.0;
var rad2 = lat2 * Math.PI / 180.0;
var a = rad1 - rad2;
var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
var r = 6378137;
return r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))
},
//设置高亮样式
polygonStyle: ()=>{
var style = new Style({
fill: new Fill({ //矢量图层填充颜色,以及透明度
color: 'rgba(220, 20, 60, 1)'
}),
stroke: new Stroke({ //边界样式
lineDash:[6],//注意:该属性为虚线效果,在IE10以上版本才有效果
color: '#FF0000',
width: 2
}),
text: new Text({ //文本样式
font: '20px Verdana,sans-serif',
// text:feature.attr.dmaName,
fill: new Fill({
color: '#FF0000'
})
})
});
return style;
}
}
};
</script>
<style>
#map{height:100%;}
/*隐藏ol的一些自带元素*/
.ol-attribution,.ol-zoom { display: none;}
.ol-popup {
position: absolute;
background-color: #fff;
-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: 12px;
left: -50px;
min-width: 280px;
}
.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: #fff;
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>
App.vue代码:
<template>
<div id="app">
<olmap />
</div>
</template>
<script>
import olmap from './components/olmap.vue'
export default {
name: 'app',
components: {
olmap
}
}
</script>
<style>
*{padding:0; margin:0;}
html,body{
height: 100%;
}
#app {
height: 100%;
}
</style>
2.运行
npm run serve
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“vue集成openlayers加载geojson并实现点击弹窗教程”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新动态
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]