原文链接及内容
这是一个将ol/interaction/Draw
绘制交互组件与ol/interaction/Modify
修改交互组件一起使用的示例。
main.js
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 import Map from 'ol/Map.js' ;import View from 'ol/View.js' ;import {Draw , Modify , Snap } from 'ol/interaction.js' ;import {OSM , Vector as VectorSource } from 'ol/source.js' ;import {Tile as TileLayer , Vector as VectorLayer } from 'ol/layer.js' ;import {get} from 'ol/proj.js' ;const raster = new TileLayer ({ source : new OSM (), }); const source = new VectorSource ();const vector = new VectorLayer ({ source : source, style : { 'fill-color' : 'rgba(255, 255, 255, 0.2)' , 'stroke-color' : '#ffcc33' , 'stroke-width' : 2 , 'circle-radius' : 7 , 'circle-fill-color' : '#ffcc33' , }, }); const extent = get ('EPSG:3857' ).getExtent ().slice ();extent[0 ] += extent[0 ]; extent[2 ] += extent[2 ]; const map = new Map ({ layers : [raster, vector], target : 'map' , view : new View ({ center : [-11000000 , 4600000 ], zoom : 4 , extent, }), }); const modify = new Modify ({source : source});map.addInteraction (modify); let draw, snap; const typeSelect = document .getElementById ('type' );function addInteractions ( ) { draw = new Draw ({ source : source, type : typeSelect.value , }); map.addInteraction (draw); snap = new Snap ({source : source}); map.addInteraction (snap); } typeSelect.onchange = function ( ) { map.removeInteraction (draw); map.removeInteraction (snap); addInteractions (); }; addInteractions ();
界面布局文件index.html
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <!DOCTYPE html > <html lang ="en" > <head > <meta charset ="UTF-8" > <title > Draw and Modify Features</title > <link rel ="stylesheet" href ="node_modules/ol/ol.css" > <style > .map { width : 100% ; height : 400px ; } </style > </head > <body > <div id ="map" class ="map" > </div > <form > <label for ="type" > Geometry type </label > <select id ="type" > <option value ="Point" > Point</option > <option value ="LineString" > LineString</option > <option value ="Polygon" > Polygon</option > <option value ="Circle" > Circle</option > </select > </form > <script src ="https://cdn.jsdelivr.net/npm/elm-pep@1.0.6/dist/elm-pep.js" > </script > <script type ="module" src ="main.js" > </script > </body > </html >