ol-ext/examples/interaction/map.interaction.drawregular.html
2018-01-31 11:45:59 +01:00

112 lines
3.9 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/master/css/ol.css" />
<script type="text/javascript" src="https://openlayers.org/en/master/build/ol.js"></script>
<!-- ol-ext -->
<link rel="stylesheet" href="../../dist/ol-ext.css" />
<script type="text/javascript" src="../../dist/ol-ext.js"></script>
</head>
<body >
<a href="https://github.com/Viglino/ol3-ext"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></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>Alt+Shift</i>)</label>
</li><li>
<i>Shift</i>: force square features.
</li><li>
<i>Alt</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({
name: "Natural Earth",
minResolution: 306,
source: new ol.source.XYZ(
{ url: 'https://{a-d}.tiles.mapbox.com/v3/mapbox.natural-earth-hypso-bathy/{z}/{x}/{y}.png',
attributions: [new ol.Attribution({ html: '&copy; <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> ' })]
})
})
]
// 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>