291 lines
6.6 KiB
JavaScript
291 lines
6.6 KiB
JavaScript
import Map from '../src/ol/Map.js';
|
|
import Observable from '../src/ol/Observable.js';
|
|
import Overlay from '../src/ol/Overlay.js';
|
|
import {getArea, getLength} from '../src/ol/sphere.js';
|
|
import View from '../src/ol/View.js';
|
|
import LineString from '../src/ol/geom/LineString.js';
|
|
import Polygon from '../src/ol/geom/Polygon.js';
|
|
import Draw from '../src/ol/interaction/Draw.js';
|
|
import TileLayer from '../src/ol/layer/Tile.js';
|
|
import VectorLayer from '../src/ol/layer/Vector.js';
|
|
import OSM from '../src/ol/source/OSM.js';
|
|
import VectorSource from '../src/ol/source/Vector.js';
|
|
import _ol_style_Circle_ from '../src/ol/style/Circle.js';
|
|
import _ol_style_Fill_ from '../src/ol/style/Fill.js';
|
|
import _ol_style_Stroke_ from '../src/ol/style/Stroke.js';
|
|
import _ol_style_Style_ from '../src/ol/style/Style.js';
|
|
|
|
|
|
var raster = new TileLayer({
|
|
source: new OSM()
|
|
});
|
|
|
|
var source = new VectorSource();
|
|
|
|
var vector = new VectorLayer({
|
|
source: source,
|
|
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'
|
|
})
|
|
})
|
|
})
|
|
});
|
|
|
|
|
|
/**
|
|
* Currently drawn feature.
|
|
* @type {ol.Feature}
|
|
*/
|
|
var sketch;
|
|
|
|
|
|
/**
|
|
* The help tooltip element.
|
|
* @type {Element}
|
|
*/
|
|
var helpTooltipElement;
|
|
|
|
|
|
/**
|
|
* Overlay to show the help messages.
|
|
* @type {ol.Overlay}
|
|
*/
|
|
var helpTooltip;
|
|
|
|
|
|
/**
|
|
* The measure tooltip element.
|
|
* @type {Element}
|
|
*/
|
|
var measureTooltipElement;
|
|
|
|
|
|
/**
|
|
* Overlay to show the measurement.
|
|
* @type {ol.Overlay}
|
|
*/
|
|
var measureTooltip;
|
|
|
|
|
|
/**
|
|
* Message to show when the user is drawing a polygon.
|
|
* @type {string}
|
|
*/
|
|
var continuePolygonMsg = 'Click to continue drawing the polygon';
|
|
|
|
|
|
/**
|
|
* Message to show when the user is drawing a line.
|
|
* @type {string}
|
|
*/
|
|
var continueLineMsg = 'Click to continue drawing the line';
|
|
|
|
|
|
/**
|
|
* Handle pointer move.
|
|
* @param {ol.MapBrowserEvent} evt The event.
|
|
*/
|
|
var pointerMoveHandler = function(evt) {
|
|
if (evt.dragging) {
|
|
return;
|
|
}
|
|
/** @type {string} */
|
|
var helpMsg = 'Click to start drawing';
|
|
|
|
if (sketch) {
|
|
var geom = (sketch.getGeometry());
|
|
if (geom instanceof Polygon) {
|
|
helpMsg = continuePolygonMsg;
|
|
} else if (geom instanceof LineString) {
|
|
helpMsg = continueLineMsg;
|
|
}
|
|
}
|
|
|
|
helpTooltipElement.innerHTML = helpMsg;
|
|
helpTooltip.setPosition(evt.coordinate);
|
|
|
|
helpTooltipElement.classList.remove('hidden');
|
|
};
|
|
|
|
|
|
var map = new Map({
|
|
layers: [raster, vector],
|
|
target: 'map',
|
|
view: new View({
|
|
center: [-11000000, 4600000],
|
|
zoom: 15
|
|
})
|
|
});
|
|
|
|
map.on('pointermove', pointerMoveHandler);
|
|
|
|
map.getViewport().addEventListener('mouseout', function() {
|
|
helpTooltipElement.classList.add('hidden');
|
|
});
|
|
|
|
var typeSelect = document.getElementById('type');
|
|
|
|
var draw; // global so we can remove it later
|
|
|
|
|
|
/**
|
|
* Format length output.
|
|
* @param {ol.geom.LineString} line The line.
|
|
* @return {string} The formatted length.
|
|
*/
|
|
var formatLength = function(line) {
|
|
var length = getLength(line);
|
|
var output;
|
|
if (length > 100) {
|
|
output = (Math.round(length / 1000 * 100) / 100) +
|
|
' ' + 'km';
|
|
} else {
|
|
output = (Math.round(length * 100) / 100) +
|
|
' ' + 'm';
|
|
}
|
|
return output;
|
|
};
|
|
|
|
|
|
/**
|
|
* Format area output.
|
|
* @param {ol.geom.Polygon} polygon The polygon.
|
|
* @return {string} Formatted area.
|
|
*/
|
|
var formatArea = function(polygon) {
|
|
var area = getArea(polygon);
|
|
var output;
|
|
if (area > 10000) {
|
|
output = (Math.round(area / 1000000 * 100) / 100) +
|
|
' ' + 'km<sup>2</sup>';
|
|
} else {
|
|
output = (Math.round(area * 100) / 100) +
|
|
' ' + 'm<sup>2</sup>';
|
|
}
|
|
return output;
|
|
};
|
|
|
|
function addInteraction() {
|
|
var type = (typeSelect.value == 'area' ? 'Polygon' : 'LineString');
|
|
draw = new Draw({
|
|
source: source,
|
|
type: type,
|
|
style: new _ol_style_Style_({
|
|
fill: new _ol_style_Fill_({
|
|
color: 'rgba(255, 255, 255, 0.2)'
|
|
}),
|
|
stroke: new _ol_style_Stroke_({
|
|
color: 'rgba(0, 0, 0, 0.5)',
|
|
lineDash: [10, 10],
|
|
width: 2
|
|
}),
|
|
image: new _ol_style_Circle_({
|
|
radius: 5,
|
|
stroke: new _ol_style_Stroke_({
|
|
color: 'rgba(0, 0, 0, 0.7)'
|
|
}),
|
|
fill: new _ol_style_Fill_({
|
|
color: 'rgba(255, 255, 255, 0.2)'
|
|
})
|
|
})
|
|
})
|
|
});
|
|
map.addInteraction(draw);
|
|
|
|
createMeasureTooltip();
|
|
createHelpTooltip();
|
|
|
|
var listener;
|
|
draw.on('drawstart',
|
|
function(evt) {
|
|
// set sketch
|
|
sketch = evt.feature;
|
|
|
|
/** @type {ol.Coordinate|undefined} */
|
|
var tooltipCoord = evt.coordinate;
|
|
|
|
listener = sketch.getGeometry().on('change', function(evt) {
|
|
var geom = evt.target;
|
|
var output;
|
|
if (geom instanceof Polygon) {
|
|
output = formatArea(geom);
|
|
tooltipCoord = geom.getInteriorPoint().getCoordinates();
|
|
} else if (geom instanceof LineString) {
|
|
output = formatLength(geom);
|
|
tooltipCoord = geom.getLastCoordinate();
|
|
}
|
|
measureTooltipElement.innerHTML = output;
|
|
measureTooltip.setPosition(tooltipCoord);
|
|
});
|
|
}, this);
|
|
|
|
draw.on('drawend',
|
|
function() {
|
|
measureTooltipElement.className = 'tooltip tooltip-static';
|
|
measureTooltip.setOffset([0, -7]);
|
|
// unset sketch
|
|
sketch = null;
|
|
// unset tooltip so that a new one can be created
|
|
measureTooltipElement = null;
|
|
createMeasureTooltip();
|
|
Observable.unByKey(listener);
|
|
}, this);
|
|
}
|
|
|
|
|
|
/**
|
|
* Creates a new help tooltip
|
|
*/
|
|
function createHelpTooltip() {
|
|
if (helpTooltipElement) {
|
|
helpTooltipElement.parentNode.removeChild(helpTooltipElement);
|
|
}
|
|
helpTooltipElement = document.createElement('div');
|
|
helpTooltipElement.className = 'tooltip hidden';
|
|
helpTooltip = new Overlay({
|
|
element: helpTooltipElement,
|
|
offset: [15, 0],
|
|
positioning: 'center-left'
|
|
});
|
|
map.addOverlay(helpTooltip);
|
|
}
|
|
|
|
|
|
/**
|
|
* Creates a new measure tooltip
|
|
*/
|
|
function createMeasureTooltip() {
|
|
if (measureTooltipElement) {
|
|
measureTooltipElement.parentNode.removeChild(measureTooltipElement);
|
|
}
|
|
measureTooltipElement = document.createElement('div');
|
|
measureTooltipElement.className = 'tooltip tooltip-measure';
|
|
measureTooltip = new Overlay({
|
|
element: measureTooltipElement,
|
|
offset: [0, -15],
|
|
positioning: 'bottom-center'
|
|
});
|
|
map.addOverlay(measureTooltip);
|
|
}
|
|
|
|
|
|
/**
|
|
* Let user change the geometry type.
|
|
*/
|
|
typeSelect.onchange = function() {
|
|
map.removeInteraction(draw);
|
|
addInteraction();
|
|
};
|
|
|
|
addInteraction();
|