Using an Extent interaction to draw an extent.
This example shows how to use an Extent
interaction to draw a modifiable extent.
Use Shift+Drag
to draw an extent.
Shift+Drag
on the corners or edges of the extent to resize it. Shift+Click
off the extent to remove it.
import 'ol/ol.css';
import ExtentInteraction from 'ol/interaction/Extent';
import GeoJSON from 'ol/format/GeoJSON';
import Map from 'ol/Map';
import View from 'ol/View';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {shiftKeyOnly} from 'ol/events/condition';
var vectorSource = new VectorSource({
url: 'data/geojson/countries.geojson',
format: new GeoJSON(),
});
var map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
new VectorLayer({
source: vectorSource,
}) ],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
}),
});
var extent = new ExtentInteraction({condition: shiftKeyOnly});
map.addInteraction(extent);
<!DOCTYPE html>
<html lang="en">
<head>
<title>Extent Interaction</title>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script src="main.js"></script>
</body>
</html>
{
"name": "extent-interaction",
"dependencies": {
"ol": "6.4.3"
},
"devDependencies": {
"parcel": "1.11.0"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --experimental-scope-hoisting --public-url . index.html"
}
}