电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

vue集成openlayers加載geojson并實(shí)現(xiàn)點(diǎn)擊彈窗教程

瀏覽:36日期:2022-11-18 11:15:15

本文實(shí)例為大家分享了vue+openlayers加載geojson并實(shí)現(xiàn)點(diǎn)擊彈窗教程,供大家參考,具體內(nèi)容如下

第一步:安裝vue-cli

cnpm install -g @vue/cli

第二步:新建一個(gè)項(xiàng)目

1.新建項(xiàng)目 (vue-openlayers為項(xiàng)目名),并選擇default模版

vue create vue-openlayers

2.安裝openlayers

cnpm i -S ol

第三步:寫業(yè)務(wù)代碼

1.刪除掉HelloWorld.vue 新建 olmap.vue組件

components/olmap.vue代碼:

<template> <div ref='rootmap'> <div class='vm'> <!-- <h2 class='h-title'>彈窗 popup</h2> --> <!-- 彈窗元素 --> <div ref='popup'> <a href='http://www.hdgsjgj.cn/bcjs/10823.html#' @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?service=WFS&version=1.0.0&request=GetFeature&typeName=workhome%3A28f&outputFormat=application%2Fjson’, format: new GeoJSON(), // features: Feature, // strategy: bbox }) var wfsVectorLayer = new VectorLayer({ style: new Style({ stroke: new Stroke({ // color: ’blue’, color: ’rgba(30,144,255)’, width: 3 }), fill: new Fill({ color: ’rgba(0, 0, 255, 0.1)’ }) }), source: wfsVectorSource, visible:true, }) this.map = new Map({ target: '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. 創(chuàng)建Overlay圖層 that.overlayer = new Overlay({ element: this.$refs.popup, // 彈窗標(biāo)簽,在html里 autoPan: true, // 如果彈窗在底圖邊緣時(shí),底圖會(huì)移動(dòng) autoPanAnimation: { // 底圖移動(dòng)動(dòng)畫 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) } }, // 關(guān)閉彈窗 closePopup: function(){ console.log(this) // 把彈窗位置設(shè)置為undefined,并清空坐標(biāo)數(shù)據(jù) this.overlayer.setPosition(undefined) this.currentCoordinate = null }, //計(jì)算兩點(diǎn)之間距離 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))) }, //設(shè)置高亮樣式 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.運(yùn)行

npm run serve

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 合景一建-无尘车间设计施工_食品医药洁净车间工程装修总承包公司 | 整合营销推广|营销网络推广公司|石家庄网站优化推广公司|智营销 好物生环保网、环保论坛 - 环保人的学习交流平台 | 成都离婚律师|成都结婚律师|成都离婚财产分割律师|成都律师-成都离婚律师网 | 窖井盖锯圆机_锯圆机金刚石锯片-无锡茂达金刚石有限公司 | 好物生环保网、环保论坛 - 环保人的学习交流平台 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 科客,主见不成见| 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 哲力实业_专注汽车涂料汽车漆研发生产_汽车漆|修补油漆品牌厂家 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 药品仓库用除湿机-变电站用防爆空调-油漆房用防爆空调-杭州特奥环保科技有限公司 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛婚外情取证-青岛王军侦探事务所 | 股票入门基础知识_股票知识_股票投资大师_格雷厄姆网 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 天然鹅卵石滤料厂家-锰砂滤料-石英砂滤料-巩义东枫净水 | LZ-373测厚仪-华瑞VOC气体检测仪-个人有毒气体检测仪-厂家-深圳市深博瑞仪器仪表有限公司 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 杭州|上海贴标机-百科| 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | 智能案卷柜_卷宗柜_钥匙柜_文件流转柜_装备柜_浙江福源智能科技有限公司 | 工控机-图像采集卡-PoE网卡-人工智能-工业主板-深圳朗锐智科 | 震动筛选机|震动分筛机|筛粉机|振筛机|振荡筛-振动筛分设备专业生产厂家高服机械 | 康明斯发电机,上柴柴油发电机,玉柴柴油发电机组_海南重康电力官网 | 上海宿田自动化设备有限公司-双面/平面/单面贴标机 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 天津云仓-天津仓储物流-天津云仓一件代发-顺东云仓 | 上海新光明泵业制造有限公司-电动隔膜泵,气动隔膜泵,卧式|立式离心泵厂家 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 高低温试验房-深圳高低温湿热箱-小型高低温冲击试验箱-爱佩试验设备 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | 手机游戏_热门软件app下载_好玩的安卓游戏下载基地-吾爱下载站 | 济南冷库安装-山东冷库设计|建造|冷库维修-山东齐雪制冷设备有限公司 | 模具ERP_模具管理系统_模具mes_模具进度管理_东莞市精纬软件有限公司 | 低温等离子清洗机(双气路进口)-嘉润万丰| 智慧水务|智慧供排水利信息化|水厂软硬件系统-上海敢创 | 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | 石膏基自流平砂浆厂家-高强石膏基保温隔声自流平-轻质抹灰石膏粉砂浆批发-永康市汇利建设有限公司 | 海外仓系统|国际货代系统|退货换标系统|WMS仓储系统|海豚云 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 |