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