ol-ext/examples/interaction/map.interaction.drawregular.html
2021-03-20 08:30:19 +01:00

108 lines
3.7 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<!--
Copyright (c) 2016-2018 Jean-Marc VIGLINO,
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
<title>ol-ext: DrawRegular interaction</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="an OL3 interaction to draw regular polygon" />
<meta name="keywords" content="ol3, interaction, draw, regular, rectangle, circle, triangle" />
<link rel="stylesheet" href="../style.css" />
<!-- jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- Openlayers -->
<link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
<script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
<!-- ol-ext -->
<link rel="stylesheet" href="../../dist/ol-ext.css" />
<script type="text/javascript" src="../../dist/ol-ext.js"></script>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
</head>
<body >
<a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>
<a href="../../index.html">
<h1>ol-ext: DrawRegular interaction</h1>
</a>
<div class="info">
<b>ol.interaction.DrawRegular</b> is an interaction to draw regular polygon (circle/ellipse, triangle, square/rectangles, etc).
</div>
<!-- Map div -->
<div id="map" style="width:600px; height:400px;"></div>
<div class="options" >
<h2>Options:</h2>
<ul><li>
<label for="sides">sides:</label>
<input id="sides" type="number" onchange="interaction.setSides(this.value);" value="4" min="0" />
<i>(&lt;3 mean circle)</i>
</li><li>
<input id="rotation" type="checkbox" checked="checked" onchange="interaction.canRotate($(this).prop('checked'));" />
<label for="rotation">allow rotation (when <i>Ctrl+Shift</i>)</label>
</li><li>
<i>Shift</i>: force square features.
</li><li>
<i>Ctrl</i>: center features.
</li></ul>
<div style="background:white; padding:0 0.45em;"><span id="info"></span>&nbsp;</div>
</div>
<script type="text/javascript">
// Layers
var layers = [
new ol.layer.Tile({
title:'terrain-background',
source: new ol.source.Stamen({ layer: 'terrain' })
})
]
// The map
var map = new ol.Map({
target: 'map',
view: new ol.View({
zoom: 5,
center: [261720, 5951081]
}),
controls: ol.control.defaults({ "attribution": false }),
layers: layers
});
// New vector layer
var vector = new ol.layer.Vector({
name: 'Vecteur',
source: new ol.source.Vector()
});
map.addLayer(vector);
var interaction = new ol.interaction.DrawRegular ({
source: vector.getSource(),
sides:$("#sides").val() ,
canRotate: $("#rotation").prop('checked')
});
map.addInteraction(interaction);
interaction.on('drawstart', function (e) {
// e.feature.on('change', function (){console.log('change');})
});
// Events handlers
interaction.on('drawing', function (e) {
if (e.feature.getGeometry().getArea) $('#info').html(
(e.feature.getGeometry().getArea()/1000000).toFixed(2)
+" km<sup>2</sup>");
});
interaction.on('drawend', function (e) { $('#info').text(""); });
</script>
</body>
</html>