Openlayers官網提供了不少實例供GISer參考學習,可是最新官網實例是基於Openlayers5.3版本以及ECMAScript6語言開發,而行業內大部都用的openlayers3-4版本較多,這與市場存在一個新老版本開發的銜接問題。GIS開發初學者每每無從下手,所以,這裏以snap interaction爲例分享線下實現的過程,步驟以下:javascript
1.css、js文件引用修改css
官網實例引用文件主要基於官網的庫文件,並且版本不同,相似以下: <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語句,可是要注意組件分佈位置。刪掉以下語句:html
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等(注意,實例中不單單隻有這兩句組件代碼,要全面檢查或經過瀏覽器監測)。
原代碼:java
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>ui
<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>this
<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 </label> </div> <div class="radio"> <label> <input type="radio" name="interaction" value="modify"> Modify </label> </div> <div class="form-group"> <label>Draw type </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>spa
轉載請註明出處——哦喲喲喲喲喲喲喲喲喲prototype