Openlayers官網實例線下實現的解決方法

Openlayers官網提供了很多實例供GISer參考學習,但是最新官網實例是基於Openlayers5.3版本以及ECMAScript6語言開發,而行業內大部都用的openlayers3-4版本較多,這與市場存在一個新老版本開發的銜接問題。GIS開發初學者往往無從下手,因此,這裏以snap interaction爲例分享線下實現的過程,步驟如下:

1.css、js文件引用修改

官網實例引用文件主要基於官網的庫文件,而且版本不一樣,類似如下:
<link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/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>
將上述代碼修改爲本地或自身版本(3.11.1)文件,如下:
<link  href="v3.11.1/css/ol.css" rel="stylesheet" type="text/css" />
<script src="v3.11.1/build/ol.js" type="text/javascript" ></script>

2.import語句刪除
刪掉官網實例中的import語句,但是要注意組件分佈位置。刪掉如下語句:

  import Map from 'ol/Map.js';
  import View from 'ol/View.js';
  import {Draw, Modify, Select, 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

3.修改組件位置
修改組件如TileLayer、OSM等,改爲ol.layer.Tile、ol.source.OSM等(注意,實例中不僅僅只有這兩句組件代碼,要全面檢查或通過瀏覽器監測)。
原代碼:

  var raster = new TileLayer({
    source: new OSM()
  });

修改後:

  var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

最後,將我修改的全部代碼分享如下(親測可用):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml&...;>
<!-- 定義xml namespace(命名空間)的 -->
<head>

<title>Snap Interaction</title>
<link  href="v3.11.1/css/ol.css" rel="stylesheet" type="text/css" /><!-- 只有 rel 屬性的 "stylesheet" 值得到了所有瀏覽器的支持。其他值只得到了部分地支持。 -->
<script src="v3.11.1/build/ol.js" type="text/javascript" ></script>

</head>
<body>

<div id="map" class="map"></div>
<form id="options-form" automplete="off">
  <div class="radio">
    <label>
      <input type="radio" name="interaction" value="draw" id="draw" checked>
      Draw &nbsp;
    </label>
  </div>
  <div class="radio">
    <label>
      <input type="radio" name="interaction" value="modify">
      Modify &nbsp;
    </label>
  </div>
  <div class="form-group">
    <label>Draw type &nbsp;</label>
    <select name="draw-type" id="draw-type">
      <option value="Point">Point</option>
      <option value="LineString">LineString</option>
      <option value="Polygon">Polygon</option>
      <option value="Circle">Circle</option>
    </select>
  </div>
</form>
<script>


  var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

var vector = new ol.layer.Vector({
    source: new ol.source.Vector(),
    style: new ol.style.Style({
      fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.2)'
      }),
      stroke: new ol.style.Stroke({
        color: '#ffcc33',
        width: 2
      }),
      image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
          color: '#ffcc33'
        })
      })
    })
  });

  var map = new ol.Map({
    layers: [raster, vector],
    target: 'map',
    view: new ol.View({
      center: [-11000000, 4600000],
      zoom: 4
    })
  });

  var ExampleModify = {
    init: function() {
      this.select = new ol.interaction.Select();
      map.addInteraction(this.select);

      this.modify = new ol.interaction.Modify({
        features: this.select.getFeatures()
      });
      map.addInteraction(this.modify);

      this.setEvents();
    },
    setEvents: function() {
      var selectedFeatures = this.select.getFeatures();

      this.select.on('change:active', function() {
        selectedFeatures.forEach(function(each) {
          selectedFeatures.remove(each);
        });
      });
    },
    setActive: function(active) {
      this.select.setActive(active);
      this.modify.setActive(active);
    }
  };
  ExampleModify.init();

  var optionsForm = document.getElementById('options-form');

  var ExampleDraw = {
    init: function() {
      map.addInteraction(this.Point);
      this.Point.setActive(false);
      map.addInteraction(this.LineString);
      this.LineString.setActive(false);
      map.addInteraction(this.Polygon);
      this.Polygon.setActive(false);
      map.addInteraction(this.Circle);
      this.Circle.setActive(false);
    },
    Point: new ol.interaction.Draw({
      source: vector.getSource(),
      type: 'Point'
    }),
  LineString: new ol.interaction.Draw({
      source: vector.getSource(),
      type: 'LineString'
    }),
  Polygon: new ol.interaction.Draw({
      source: vector.getSource(),
      type: 'Polygon'
    }),
  Circle: new ol.interaction.Draw({
      source: vector.getSource(),
      type: 'Circle'
    }),
    getActive: function() {
      return this.activeType ? this[this.activeType].getActive() : false;
    },
    setActive: function(active) {
      var type = optionsForm.elements['draw-type'].value;
      if (active) {
        this.activeType && this[this.activeType].setActive(false);
        this[type].setActive(true);
        this.activeType = type;
      } else {
        this.activeType && this[this.activeType].setActive(false);
        this.activeType = null;
      }
    }
  };
  ExampleDraw.init();


  optionsForm.onchange = function(e) {
    var type = e.target.getAttribute('name');
    var value = e.target.value;
    if (type == 'draw-type') {
      ExampleDraw.getActive() && ExampleDraw.setActive(true);
    } else if (type == 'interaction') {
      if (value == 'modify') {
        ExampleDraw.setActive(false);
        ExampleModify.setActive(true);
      } else if (value == 'draw') {
        ExampleDraw.setActive(true);
        ExampleModify.setActive(false);
      }
    }
  };
  ExampleDraw.setActive(true);
  ExampleModify.setActive(false);
  var snap = new ol.interaction.Snap({
    source: vector.getSource()
  });
  map.addInteraction(snap);
</script>

</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章