Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示
更新時(shí)間:2020年12月24日 10:17:41 作者:小小并不小
這篇文章主要為大家詳細(xì)介紹了Vue+penlayers實(shí)現(xiàn)多邊形繪制及展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(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)文章
詳解讓sublime text3支持Vue語(yǔ)法高亮顯示的示例
本篇文章主要介紹了讓sublime text3支持Vue語(yǔ)法高亮顯示的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
Vue中使用v-print打印出現(xiàn)空白頁(yè)問(wèn)題及解決
這篇文章主要介紹了Vue中使用v-print打印出現(xiàn)空白頁(yè)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Vuex利用state保存新聞數(shù)據(jù)實(shí)例
本篇文章主要介紹了Vuex利用state保存新聞數(shù)據(jù)實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
基于ElementUI實(shí)現(xiàn)v-tooltip指令的示例代碼
文本溢出隱藏并使用tooltip 提示的需求,相信在平時(shí)的開發(fā)中會(huì)經(jīng)常遇到,常規(guī)做法一般是使用 elementui 的 el-tooltip 組件,在每次組件更新的時(shí)候去獲取元素的寬度/高度判斷是否被隱藏,本文給大家介紹了基于ElementUI實(shí)現(xiàn)v-tooltip指令,需要的朋友可以參考下2024-09-09
vue使用iframe嵌入網(wǎng)頁(yè)的示例代碼
本篇文章主要介紹了vue使用iframe嵌入網(wǎng)頁(yè)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

