Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示
本文實(shí)例為大家分享了Vue+penlayers實(shí)現(xiàn)多邊形繪制展示代碼,供大家參考,具體內(nèi)容如下
<!--
* @Description: 繪制多邊形
* @Author: Dragon
* @Date: 2020-12-17 16:02:06
* @LastEditTime: 2020-12-18 17:20:33
* @LastEditors: Dragon
-->
<template>
<div>
<div class="query-wrap">
<el-button type="primary" @click="drawStart('Polygon')">
{{ isDraw ? "繪制區(qū)域" : "重新繪制" }}
</el-button>
</div>
<div id="map"></div>
</div>
</template>
<script>
import "ol/ol.css";
import { Map, View } from "ol";
import { Image as ImageLayer, Vector as VectorLayer } from "ol/layer";
import { ImageStatic, Vector as VectorSource } from "ol/source";
import { getCenter } from "ol/extent";
import { Projection } from "ol/proj";
import Draw from "ol/interaction/Draw";
import { Style, Fill, Stroke } from "ol/style";
import { GeoJSON } from "ol/format";
import staticMap from "@/assets/map.png";
export default {
data() {
return {
map: null, // 地圖
imgx: 0, // 當(dāng)前地圖寬
imgy: 0, // 當(dāng)前地圖高
isDraw: true, // 是否繪制
draw: null,
source: null,
vector: null,
styles: [
new Style({
stroke: new Stroke({
color: "rgba(255,0,0,0.6)",
width: 2,
}),
fill: new Fill({
color: "rgba(255,0,0,0.3)",
}),
}),
],
geojsonObject: {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [
[
[97.16862961519749, 322.26517247174047],
[117.3211820327625, 481.9353954724479],
[1.056456546810466, 489.6863771715114],
[13.458027265312012, 320.71497613192776],
[97.16862961519749, 322.26517247174047]
]
],
},
},
],
},
};
},
methods: {
// 初始化地圖
initMap() {
let extent = [0, 0, this.imgx, this.imgy];
let projection = new Projection({
code: "xkcd-image",
units: "pixels",
extent: extent,
});
let $this = this;
this.map = new Map({
target: "map",
layers: [
new ImageLayer({ // 展示地圖層
source: new ImageStatic({
url: staticMap,
projection: projection,
imageExtent: extent,
}),
}),
new VectorLayer({
source: new VectorSource({
features: new GeoJSON().readFeatures($this.geojsonObject),
}),
style: $this.styles,
}),
],
view: new View({
projection: projection,
center: getCenter(extent),
zoom: 2,
maxZoom: 18,
}),
});
this.source = new VectorSource({ wrapX: false })
this.vector = new VectorLayer({
source: this.source,
style: this.styles
})
this.map.addLayer(this.vector)
},
// 開始繪制多邊形
drawStart(type) {
let that = this;
if(this.isDraw) {
this.isDraw = false
this.draw = new Draw({
source: this.source,
type: type,
});
this.map.addInteraction(this.draw);
this.draw.on("drawend", function (evt) {
that.drawingEnd(evt);
});
} else {
this.source.clear()
this.map.removeInteraction(this.draw);
this.isDraw = true
}
},
// 構(gòu)建多邊形結(jié)束
drawingEnd(evt) {
let that = this
const geo = evt.feature.getGeometry();
const t = geo.getType();
if (t === "Polygon") {
// 獲取坐標(biāo)點(diǎn)
const points = geo.getCoordinates();
console.warn(points, "繪制結(jié)束,點(diǎn)坐標(biāo)")
this.map.removeInteraction(this.draw); // 移除繪制
}
},
},
mounted() {
let that = this;
let img = new Image();
setTimeout(function() {
img.src = staticMap;
img.onload = function (res) {
that.imgx = res.target.width;
that.imgy = res.target.height;
that.initMap();
};
}, 500)
},
};
</script>
<style>
#map {
width: 100%;
height: calc(100vh - 50px);
}
</style>
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue select選擇框數(shù)據(jù)變化監(jiān)聽方法
今天小編就為大家分享一篇vue select選擇框數(shù)據(jù)變化監(jiān)聽方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue項(xiàng)目npm?run?build打包dist文件及打包后空白解決辦法
npm run build 這個(gè)命令會(huì)執(zhí)行Vue CLI中預(yù)定義的打包配置,并將打包后的文件存放在"dist"文件夾中,這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目npm?run?build打包dist文件及打包后空白的解決辦法,需要的朋友可以參考下2023-10-10
解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題
今天小編就為大家分享一篇解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue打包部署到springboot的實(shí)現(xiàn)示例
項(xiàng)目開發(fā)中,一般我們都會(huì)使用SpringBoot+Vue進(jìn)行前后端開發(fā),本文主要介紹了vue打包部署到springboot的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Vue-router優(yōu)化import引入過(guò)多導(dǎo)致index文件臃腫問(wèn)題
這篇文章主要為大家介紹了Vue-router優(yōu)化import引入過(guò)多導(dǎo)致index文件臃腫問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
vite打包出現(xiàn)?"default"?is?not?exported?by?"
這篇文章主要給大家介紹了關(guān)于vite打包出現(xiàn)?"default"?is?not?exported?by?"node_modules/...問(wèn)題的解決辦法,文中通過(guò)代碼將解決的辦法介紹的非常詳細(xì),對(duì)同樣遇到這個(gè)問(wèn)題的朋友具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06
Vue自定義el-table表格表頭高度的多種實(shí)現(xiàn)方法
在Vue項(xiàng)目中,使用Element?UI的el-table組件可以輕松創(chuàng)建功能豐富的表格,然而,默認(rèn)情況下,el-table的表頭高度是固定的,本文將詳細(xì)介紹如何自定義el-table表頭的高度,提供多種實(shí)現(xiàn)方法,需要的朋友可以參考下2024-10-10
Vue中為什么要引入render函數(shù)的實(shí)現(xiàn)
本文主要介紹了Vue中為什么要引入render函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
element 穿梭框性能優(yōu)化的實(shí)現(xiàn)
本文主要介紹了element 穿梭框性能優(yōu)化,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
vue列表單項(xiàng)展開收縮功能之this.$refs的詳解
這篇文章主要介紹了vue列表單項(xiàng)展開收縮功能之this.$refs的詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

