From 1c26c53c110b9dd594a912adc623638171ed6e52 Mon Sep 17 00:00:00 2001 From: maltaesousa Date: Tue, 21 May 2019 15:03:58 +0200 Subject: [PATCH] add markers --- img/marker.svg | 7 +++++ index.html | 52 +++++++++++++++++++++++++++++++++++ js/sitnlayers.js | 71 ++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 img/marker.svg diff --git a/img/marker.svg b/img/marker.svg new file mode 100644 index 0000000..47bc9f7 --- /dev/null +++ b/img/marker.svg @@ -0,0 +1,7 @@ + + + + diff --git a/index.html b/index.html index 28c5a3b..cd257fb 100644 --- a/index.html +++ b/index.html @@ -129,6 +129,58 @@

Exemple 3


+

Exemple 4

+

Ajouter, modifier et supprimer un marker selon une paire de coordonnées Est-Nord. Recenter la carte.

+
+
+ + +
+
+ + +
+
+
+ + + + +
+
+ + + +
+ diff --git a/js/sitnlayers.js b/js/sitnlayers.js index 1b1d947..271148b 100644 --- a/js/sitnlayers.js +++ b/js/sitnlayers.js @@ -6,7 +6,7 @@ const _crs = 'EPSG:2056' const _WMTSurl = 'https://sitn.ne.ch/web_getcapabilities/WMTSGetCapabilities95.xml' const _drawSource = new ol.source.Vector() - const _vectorLayer = new ol.layer.Vector({ source: _drawSource }) + const _markerSource = new ol.source.Vector() const _sitnBaseLayers = { 'plan_ville': 'Plan de ville', 'plan_cadastral': 'Plan cadastral', @@ -17,6 +17,8 @@ var _target var _selectTarget var _drawSimpleGeom = false + var _map = false + var _markerColor sitnLayers.sitnCurrentBaseLayer = new ol.layer.Tile() @@ -72,11 +74,12 @@ _selectTarget = options['selectTarget'] _drawSimpleGeom = options['drawSimpleGeom'] // controls wether or not an user can draw multiple geometries let _mainbar - let _map = new ol.Map({ + _map = new ol.Map({ target: _target, layers: [ sitnLayers.sitnCurrentBaseLayer, - _vectorLayer + new ol.layer.Vector({ source: _drawSource }), + new ol.layer.Vector({ source: _markerSource }) ], view: sitnLayers.view }) @@ -123,7 +126,7 @@ if (!features.getLength()) console.log('Select an object first...') else console.log(features.getLength() + ' object(s) deleted.') for (var i = 0, f; (f = features.item(i)); i++) { - _vectorLayer.getSource().removeFeature(f) + _drawSource.removeFeature(f) } selectCtrl.getInteraction().getFeatures().clear() } @@ -147,7 +150,7 @@ title: 'Point', interaction: new ol.interaction.Draw({ type: 'Point', - source: _vectorLayer.getSource() + source: _drawSource }) }) editbar.addControl(pedit) @@ -159,7 +162,7 @@ title: 'LineString', interaction: new ol.interaction.Draw({ type: 'LineString', - source: _vectorLayer.getSource(), + source: _drawSource, // Count inserted points geometryFunction: function (coordinates, geometry) { if (geometry) { @@ -199,7 +202,7 @@ title: 'Polygon', interaction: new ol.interaction.Draw({ type: 'Polygon', - source: _vectorLayer.getSource(), + source: _drawSource, // Count inserted points geometryFunction: function (coordinates, geometry) { this.nbpts = coordinates[0].length @@ -240,6 +243,7 @@ }); } } + sitnLayers.loadWKT = function (wkt) { let format = new ol.format.WKT() let data = wkt; @@ -247,16 +251,65 @@ dataProjection: _crs, featureProjection: _crs }) - _vectorLayer.getSource().addFeatures(features) + _drawSource.addFeatures(features) } sitnLayers.getWKTData = function () { - let features = _vectorLayer.getSource().getFeatures() + let features = _drawSource.getFeatures() if (features) { let wktData = new ol.format.WKT().writeFeatures(features) return wktData } } + + /** + * Adds a marker based on coordinates: an array of 2 numbers + * and clears another existing maker before. The color is optional + */ + sitnLayers.addMarker = function (coordinates, color) { + _markerColor = color + _markerSource.clear() + let marker = new ol.Feature({ + geometry: new ol.geom.Point(coordinates) + }) + marker.setStyle(new ol.style.Style({ + image: new ol.style.Icon({ + anchor: [0.5, 1], + color: _markerColor || '#8959A8', + opacity: 1, + src: '../img/marker.svg' + }) + })) + _markerSource.addFeature(marker) + } + + /** + * Updates marker position based + * on coordinates: an array of 2 numbers + */ + sitnLayers.updateMarker = function (coordinates) { + sitnLayers.addMarker(coordinates) + } + + /** + * Removes marker + */ + sitnLayers.removeMarker = function () { + _markerSource.clear() + } + + /** + * Recenters the map based + * on coordinates: an array of 2 numbers + */ + sitnLayers.recenterMap = function (coordinates, zoomLevel) { + if (_map) { + let view = _map.getView() + view.setCenter(coordinates) + view.setZoom(zoomLevel) + } + } + return sitnLayers }