三十九、openlayers官网示例Extent Interaction解析——在地图上绘制范围并获取数据

官网demo 地址:

Extent Interaction

在openlayers中可以使用ExtentInteraction添加交互事件,配合shiftKeyOnly实现按住shift键绘制边界区域。 

 const map = new Map({
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: "map",
      view: new View({
        center: [0, 0],
        zoom: 2,
      }),
    });

    const extent = new ExtentInteraction({ condition: shiftKeyOnly });
    map.addInteraction(extent);
    

按住shift绘制矩形,按住shift键点击图形删除。

监听extentchanged事件,记录绘制的矩形数据。

// 监听 extentchanged 事件,获取矩形选择范围
extent.on("extentchanged", (event) => {
    this.extentData = event.extent;
});
 
getData() {
   console.log("结果", this.extentData);
},

完整代码:

<template>
  <div class="box">
    <h1>Extent Interaction</h1>
    <div id="map"></div>
    <el-button type="primary" @click="getData">获取数据</el-button>
  </div>
</template>

<script>
import ExtentInteraction from "ol/interaction/Extent.js";
import Map from "ol/Map.js";
import OSM from "ol/source/OSM.js";
import TileLayer from "ol/layer/Tile.js";
import View from "ol/View.js";
import { shiftKeyOnly } from "ol/events/condition.js";
export default {
  name: "",
  components: {},
  data() {
    return {
      map: null,
      extentData: "",
    };
  },
  computed: {},
  created() {},
  mounted() {
    const map = new Map({
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: "map",
      view: new View({
        center: [0, 0],
        zoom: 2,
      }),
    });

    const extent = new ExtentInteraction({ condition: shiftKeyOnly });
    map.addInteraction(extent);
    // 监听 extentchanged 事件,获取矩形选择范围
    extent.on("extentchanged", (event) => {
      this.extentData = event.extent;
    });
  },
  methods: {
    getData() {
      console.log("结果", this.extentData);
    },
  },
};
</script>

<style lang="scss" scoped>
#map {
  width: 100%;
  height: 500px;
}
.box {
  height: 100%;
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 在Vue中安装OpenLayers库 ``` npm install ol ``` 2. 在Vue组件中引入OpenLayers库和样式文件 ``` import 'ol/ol.css'; import { Map, View } from 'ol'; import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'; import { OSM, Vector as VectorSource } from 'ol/source'; import { Circle as CircleStyle, Fill, Stroke, Style } from 'ol/style'; ``` 3. 创建一个地图容器 ``` <template> <div id="map" style="height: 400px; width: 100%;"></div> </template> ``` 4. 在Vue组件中创建地图对象和图层 ``` <script> export default { name: 'MapComponent', mounted() { // 创建地图容器 const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // 创建矢量图层 const vectorLayer = new VectorLayer({ source: new VectorSource(), style: new Style({ image: new CircleStyle({ radius: 5, fill: new Fill({ color: 'red' }), stroke: new Stroke({ color: 'white', width: 1 }) }) }) }); // 添加矢量图层到地图 map.addLayer(vectorLayer); // 给定一组归一化坐标数据 const coordinates = [ [0.5, 0.5], [0.6, 0.6], [0.7, 0.4], [0.3, 0.8], [0.9, 0.2] ]; // 将归一化坐标转换为地理坐标 const features = coordinates.map(coord => { return new Feature({ geometry: new Point(fromExtent([coord[0], coord[1], coord[0], coord[1]])) }); }); // 添加要素到矢量图层 vectorLayer.getSource().addFeatures(features); } } </script> ``` 5. 在地图画布上绘制图形 使用`fromExtent`方法将归一化坐标转换为地理坐标,然后创建一个`Point`要素,并添加到矢量图层中。 在样式中使用`CircleStyle`来定义点的样式,并将其作为`Style`对象的`image`属性传递给矢量图层。 最终效果如下图所示: ![map](https://i.loli.net/2021/07/17/1Y8KuV7sGQlJ2wN.png)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值