vue2+OpenLayers 天地图上画图层(3)把当前的图层放上去

news/2024/9/22 19:02:49/

点击画笔按钮
开始框画
取当前的裁剪图层为后面的图层
恢复画笔
这里提前引入了Draw

javascript">import Draw, { createRegularPolygon, createBox } from "ol/interaction/Draw";

在这里插入图片描述
在这里插入图片描述
代码如下

javascript">
<template><div class="container"><div id="vue-openlayers" class="map-x"></div><divid="info-box"class="info-box"style="width: 100px; height: 100px"></div><div id="canv" style="width: 100px; height: 100px"></div><div class="btn"><el-button type="primary" @click="drawLine"> 画塘口</el-button><el-button type="primary" @click="huanyuan"> 还原 </el-button></div></div>
</template>
<script>
import "ol/ol.css";
import { Map, View, style, Feature, geom, Overlay } from "ol";
import TileLayer from "ol/layer/Tile";
import XYZ from "ol/source/XYZ";
import { Vector as VectorSource } from "ol/source";
import VectorLayer from "ol/layer/Vector";
import { Point, LineString } from "ol/geom";
import { Style, Icon, Stroke, Text, Fill } from "ol/style";
import logo from "@/assets/logo.png";
import * as ol from "ol";
import "ol-ext/dist/ol-ext.css";import Draw, { createRegularPolygon, createBox } from "ol/interaction/Draw";
export default {name: "FirstMap",data() {return {map: null,draw: null,maskLayer: null,logo,layers: [],};},methods: {initMap() {let that = this;// 将图标样式应用到点要素const features = [];const point = new Point([108.56, 34.15]); // 修改坐标格式const feature = new Feature({geometry: point,custom: { data: "123", type: "icon" },type: "icon",});feature.setStyle([new Style({image: new Icon({crossOrigin: "anonymous",src: this.logo,// size: [40, 40],scale: 0.2, // 图标缩放比例}),}),]);features.push(feature);//设置地图的数据源const source = new VectorSource({features,});let markLayerPoints = new VectorLayer({source: source,});let map = new Map({target: "vue-openlayers",layers: [new TileLayer({source: new XYZ({url: "https://gdtc.shipxy.com/tile.g?l=en&m=d&z={z}&x={x}&y={y}",}),}),markLayerPoints, // 确保图层顺序正确// vectorLayers,],view: new View({projection: "EPSG:4326",center: [108.56, 34.15], // 修改中心坐标格式zoom: 6,}),});this.map = map;},// 变成画笔drawLine() {let that = this;const draw = new Draw({source: new VectorSource(),type: "Polygon",name: "huizong",});that.map.addInteraction(draw);that.draw = draw;that.draw.on("drawend", function (event) {const polygon = event.feature.getGeometry();that.clipMap(polygon);});},// 取当前地图的裁剪图层为下一次的图层clipMap(polygon) {console.log(polygon,'polygon');const extent = polygon.getExtent();console.log(extent,'extentextentextent');// 设置图层的可见范围const layer = this.map.getLayers().item(0); // 获取第一个图层console.log(layer, "layerlayerlayer");layer.setExtent(extent);// 更新地图视图以适应绘制的区域this.map.getView().fit(extent, { padding: [50, 50, 50, 50], duration: 500 });this.map.updateSize();this.cancelDraw();},// 还原画笔cancelDraw() {let that = this;// 从地图中移除绘制交互that.map.removeInteraction(that.draw);// 如果需要,可以清除源中的所有要素// that.draw.getSource().clear(true);},huanyuan() {console.log(this.map.getLayers().getArray(), "layerlayerlayer");const layer = this.map.getLayers().item(0); // 获取第一个图层layer.setExtent(this.map.getView().getProjection().getExtent());},
},mounted() {this.initMap();},
};
</script>
<style scoped lang="scss">
.input {position: fixed;top: 10px;right: 10px;border-radius: 10px;background: #fff;display: flex;flex-direction: column;padding: 5px;padding-bottom: 10px;> * {margin-top: 10px;display: flex;align-items: center;}
}
</style><style scoped lang="scss">
.container {position: relative;.btn {position: absolute;left: 4%;top: 1%;}
}#vue-openlayers {width: 100vw;height: 100vh;
}h3 {line-height: 40px;
}/* 隐藏信息盒子的初始样式 */
#info-box {display: none;position: absolute;background: white;border: 1px solid black;padding: 10px;border-radius: 5px;font-size: 14px;pointer-events: none; /* 防止信息盒子影响鼠标事件 */
}
</style>

http://www.ppmy.cn/news/1508933.html

相关文章

PDF转markdown工具:magic-pdf

1. magic-pdf 环境安装 conda create -n MinerU python3.10 conda activate MinerU pip install boto3>1.28.43 -i https://pypi.tuna.tsinghua.edu.cn/simple/ pip install magic-pdf[full]0.7.0b1 --extra-index-url https://wheels.myhloli.com -i https://pypi.tuna.t…

[ACP云计算]易混淆知识点(考题总结)

ECS 1、ECS所在的地域叫做Region&#xff0c;可用区叫做Zone。每个地域完全独立&#xff0c;每个可用区完全隔离。 同地域下不同可用区之间的距离大概在数十公里。 2、伸缩组 ECS实例和伸缩组必须在同一个地域&#xff0c;但是对可用区没有要求。 ECS同一时刻只能加入到一…

Java多线程练习(6)

MultiProcessingExercise6 package MultiProcessingExercise620240813;import java.util.ArrayList; import java.util.Collections;public class MultiProcessingExercise6 {public static void main(String[] args) {// 需求&#xff1a;// 每次抽的过程中&#xff0c;不打印…

基于微信小程序的心理测评平台设计与实现

基于微信小程序的心理测评平台设计与实现 Design and Implementation of a Psychological Assessment Platform based on WeChat Mini Program 完整下载链接:基于微信小程序的心理测评平台设计与实现 文章目录 基于微信小程序的心理测评平台设计与实现摘要第一章 引言1.1 研究…

服务端渲染(SSR)在现代网站开发中的地位与应用

目录 服务端渲染的概念 服务端渲染的优势 1. 提升首屏加载速度 2. 有利于搜索引擎优化&#xff08;SEO&#xff09; 3. 改善用户体验 服务端渲染的局限性 1. 服务器负载 2. 开发复杂性 3. 延迟更新 现代应用案例 技术实现 SSR的工作流程 SSR与其他技术的比较 客户…

Leetcode面试经典150题-236.二叉树的最低公共祖先

解法都在代码里&#xff0c;不懂就留言或者私信 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/ class Solution {/**题目分析&#xff1a;本题是经典的二…

spring过滤器和拦截器的区别

1出身不同。 过滤器来自servlet&#xff0c;拦截器来自spring框架。 2触发时机 不同请求的执行顺序是&#xff1a;请求进入容器 > 进入过滤器 > 进入 Servlet > 进入拦截器 > 执行控制器 过滤器先执行&#xff0c;会在servlet请求之前和相应之后进行处理。 拦…

自助创建 1Panel 应用

自助创建 1Panel 应用 前言 1Panel 作为一款开源的 Linux 服务器运维管理面板&#xff0c;其优质的 应用商店 想必也是很多人喜爱它的原因&#xff0c;除了官方的 应用列表 &#xff0c;开源社区内也涌现出了许多优质的第三方应用商店资源&#xff0c;比如 okxlin/appstore 等…