首页 > 其他分享 >[openlayers03]——Modifying features

[openlayers03]——Modifying features

时间:2023-02-06 16:12:57浏览次数:64  
标签:openlayers03 ol const features map Modifying source import new

Modifying features

1.说明

结果展示

chrome-capture-2023-1-6

2. 步骤

  • 导入Modify
import Modify from 'ol/interaction/Modify';
  • 创建地图、设置矢量源并加入地图
// 下面用于确定地图中心 center
const center = [114.1692, 30.494]; //EPSG:4326
const transformedCenter = transform(center, 'EPSG:4326', 'EPSG:3857');
const view = new View({
  center: transformedCenter,
  zoom: 10
});

// 创建地图
const map = new Map({
    target: 'map',
    view: view
  });

// 1.矢量源
const source = new VectorSource({
  format: new GeoJSON(),
  url:'./data/hubei.geojson'
})

// 2. 用我们的矢量源创建一个新的layer,并将其添加到地图中
const layer = new VectorLayer({
    source:source,
});
map.addLayer(layer);
  • 关键: 创建一个连接到向量源的新交互Modify,并将其添加到 map
// 3. 创建一个连接到向量源的新交互,并将其添加到 map
map.addInteraction(
  new Modify({
    source: source,
  })
);

3 .完整代码

  • js:
import './style.css';
import Map from 'ol/Map.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import View from 'ol/View.js';
import VectorLayer from 'ol/layer/vector';
import VectorSource from 'ol/source/Vector';
import { transform } from 'ol/proj';
import Modify from 'ol/interaction/Modify';
// 下面用于确定地图中心 center
const center = [114.1692, 30.494]; //EPSG:4326
const transformedCenter = transform(center, 'EPSG:4326', 'EPSG:3857');
const view = new View({
  center: transformedCenter,
  zoom: 10
});

// 创建地图
const map = new Map({
    target: 'map',
    view: view
  });

// 1.矢量源
const source = new VectorSource({
  format: new GeoJSON(),
  url:'./data/hubei.geojson'
})

// 2. 用我们的矢量源创建一个新的layer,并将其添加到地图中
const layer = new VectorLayer({
    source:source,
});
map.addLayer(layer);

// 3. 创建一个连接到向量源的新交互,并将其添加到 map
map.addInteraction(
  new Modify({
    source: source,
  })
);

  • html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Quick Start</title>
    <style>
      @import "node_modules/ol/ol.css";
    </style>
    <style>
      html, body, #map-container {
        margin: 0;
        height: 100%;
        width: 100%;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>

    <div id="map"></div>
    <script src="./main.js" type="module"></script>
  </body>

</html>

标签:openlayers03,ol,const,features,map,Modifying,source,import,new
From: https://www.cnblogs.com/aleza/p/17095694.html

相关文章