1. 程式人生 > >openlayer 繪製和修改要素

openlayer 繪製和修改要素

繪製和修改要素

<!DOCTYPE html><html>

  <head>

    <title>Draw and Modify Features</title>

    <link rel="stylesheet" href="https://openlayers.org/en/v5.1.3/css/ol.css" type="text/css">

    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->

    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>

  </head>

  <body>

    <div id="map" class="map"></div>

    <form class="form-inline">

      <label>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>

      import Map from 'ol/Map.js';

      import View from 'ol/View.js';

      import {Draw, Modify, Snap} from 'ol/interaction.js';

      import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';

      import {OSM, Vector as VectorSource} from 'ol/source.js';

      import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style.js';

      var raster = new TileLayer({

        source: new OSM()

      });

      var source = new VectorSource();

      var vector = new VectorLayer({

        source: source,

        style: new Style({

          fill: new Fill({

            color: 'rgba(255, 255, 255, 0.2)'

          }),

          stroke: new Stroke({

            color: '#ffcc33',

            width: 2

          }),

          image: new CircleStyle({

            radius: 7,

            fill: new Fill({

              color: '#ffcc33'

            })

          })

        })

      });

      var map = new Map({

        layers: [raster, vector],

        target: 'map',

        view: new View({

          center: [-11000000, 4600000],

          zoom: 4

        })

      });

      var modify = new Modify({source: source});

      map.addInteraction(modify);

      var draw, snap; // global so we can remove them later

      var 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);

      }

      /**

       * Handle change event.

       */

      typeSelect.onchange = function() {

        map.removeInteraction(draw);

        map.removeInteraction(snap);

        addInteractions();

      };

      addInteractions();

    </script>

  </body></html>