Openlayers的交互功能(七)——平移图斑

  • GIS应用中拖动旋转和缩放是编辑中比较常见的功能,在OpenLayers中平移功能可以通过ol.interaction.Translate来实现,但是其他功能没有,所以要用的openlayers的一个扩展ol-ext。

一、ol-ext

1.基本介绍

  • ol-ext是OpenLayers的扩展。主要包含控件,交互,弹出窗口等功能。类似于WEB前端的UI框架,是一个成熟的扩展,可以节省时间、避免问题。

2.安装

npm install ol-ext

3.链接

二、平移图斑

1.实现代码

<template>
  <!-- 初始化一个地图容器 -->
  <div id="map"></div>
  <div style="position: absolute; right: 50px">
    <button @click="moveFeatures()">平移</button>
  </div>
</template>

<script setup>
// 引入需要的包
import TileLayer from "ol/layer/Tile";
import { OSM } from "ol/source";
import { Feature, View } from "ol";
import { Map } from "ol";
import { onMounted } from "vue";
import { Polygon } from "ol/geom";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";

import { Fill, Icon, Stroke, Style } from "ol/style";
import { DoubleClickZoom, Draw, Modify, Select } from "ol/interaction";
import CircleStyle from "ol/style/Circle";

import ExtTransform from "ol-ext/interaction/Transform";

/**
 * 为了方便其他函数调用地图和图层,把变量定义成全局的
 */
let map; // 地图
let vectorLayer; // 存放Feataure的图层
let selectedFeature; // 选中的图斑
let select; // 选中控件
/**
 * 初始化地图
 */
const initialMap = () => {
  map = new Map({
    target: "map", // 地图放到哪个容器中,值应该是div的id
    layers: [
      new TileLayer({
        source: new OSM(), // 加载OSM底图
      }),
      new VectorLayer({
        source: drawSource,
        style: new Style({
          fill: new Fill({
            color: "rgba(255, 255, 255, 0.2)",
          }),
          stroke: new Stroke({
            // color: "#ffcc33",
            color: "#ff0033",
            width: 2,
          }),
          image: new CircleStyle({
            radius: 3,
            fill: new Fill({
              color: "#ffcc33",
            }),
          }),
        }),
      }),
    ],

    // 以EPSG:4326为坐标系定义地图的视图中心和缩放级别
    view: new View({
      projection: "EPSG:4326",
      center: [125.3574397847, 43.8865062907],
      zoom: 18,
    }),
  });
  addLayer();

  // 选中事件
  select = new Select({
    style: new Style({
      fill: new Fill({
        color: "rgba(255,0,0,0)",
      }),
      stroke: new Stroke({
        color: "rgba(0,0,255,1)",
      }),
    }),
  });
  map.addInteraction(select);

  // 监听select选中事件
  select.on("select", (evt) => {
    selectedFeature = select.getFeatures().getArray();
  });
};

const addLayer = () => {
  vectorLayer = new VectorLayer({
    source: new VectorSource(),
  });
  vectorLayer.getSource().addFeature(
    // 添加面图层
    new Feature({
      geometry: new Polygon([
        [
          [125.3579180563, 43.888298024],
          [125.3587389704, 43.887798338],
          [125.3574397847, 43.8865062907],
          [125.3579180563, 43.888298024],
        ],
      ]),
    })
  );
  map.addLayer(vectorLayer);
  map.getView().setCenter([125.3579180563, 43.888298024]);
};

const moveFeatures = () => {
  const dblClickInteraction = map
    .getInteractions()
    .getArray()
    .find((interaction) => {
      return interaction instanceof DoubleClickZoom;
    });
  map.removeInteraction(dblClickInteraction); //移除双击缩放功能,使双击只用于关闭平移状态

  let interactionDrag = new ExtTransform({
    enableRotatedTransform: false, //地图旋转时启用变换
    hitTolerance: 2, //偏移量
    translate: true, // 拖拽
    stretch: false, // 拉伸
    scale: false, // 缩放
    rotate: false, // 旋转
    translateFeature: false, //true是不显示中心位置,false是显示中心位置
    noFlip: false, //防止特征几何翻转,默认为 false
    layers: [vectorLayer], //指定可以拖拽的图层
  });
  //添加交互
  map.addInteraction(interactionDrag);

  interactionDrag.on(["translatestart"], function (evt) {});
  interactionDrag.on(["translating"], function (evt) {
    //逻辑代码
  });
  interactionDrag.on(["translateend"], function (evt) {
    //逻辑代码
  });
  map.on("dblclick", function () {
    //双击移除控件,不再平移
    map.removeInteraction(interactionDrag);
  });
};

onMounted(() => {
  // 立即执行初始化地图函数
  initialMap();
});
</script>

<style scoped>
#map {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

:deep(.ol-attribution) {
  display: none;
}
</style>

2.实现效果

  • 下面是平移图斑的效果
  • 先点击按钮,再点击图斑,即可开始平移
    平移图斑实现效果

三、结语

  • 本文对图斑的平移进行了简单的介绍,后续将继续更新Feature的框选平移操作。

  • 如有错误,请批评指正!

  • 欢迎关注星林社区文章将同步更新在星林社区中!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值