code
Snippet
language
CDN
package_2
NPM
content_copy
Copy
open_in_new
Open
var source = new ol.source. Vector ();
var drawLayer = new ol.layer. Vector ({
source: source,
style: new ol.style. Style ({
fill: new ol.style. Fill ({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new ol.style. Stroke ({ color: '#e63946' , width: 2 }),
image: new ol.style. Circle ({ radius: 6 , fill: new ol.style. Fill ({ color: '#e63946' }) })
})
});
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.3522 , 48.8566 ]), zoom: 13 })
});
var draw = new ol.interaction. Draw ({ source: source, type: 'Polygon' });
map. addInteraction (draw);
draw. on ( 'drawend' , function ( evt ) {
var geom = evt.feature. getGeometry ();
var area = ol.sphere. getArea (geom);
console. log ( 'Area: ' + (area / 1e6 ). toFixed ( 2 ) + ' km²' );
}); content_copy
Copy
open_in_new
Open
<! doctype html >
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Draw Interaction in OpenLayers</ title >
< link rel = "stylesheet" href = "https://unpkg.com/ol@10.11.0/ol.css" >
< script src = "https://unpkg.com/ol@10.11.0/dist/ol.js" ></ script >
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< script >
var source = new ol.source. Vector ();
var drawLayer = new ol.layer. Vector ({
source: source,
style: new ol.style. Style ({
fill: new ol.style. Fill ({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new ol.style. Stroke ({ color: '#e63946' , width: 2 }),
image: new ol.style. Circle ({ radius: 6 , fill: new ol.style. Fill ({ color: '#e63946' }) })
})
});
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.3522 , 48.8566 ]), zoom: 13 })
});
var draw = new ol.interaction. Draw ({ source: source, type: 'Polygon' });
map. addInteraction (draw);
draw. on ( 'drawend' , function ( evt ) {
var geom = evt.feature. getGeometry ();
var area = ol.sphere. getArea (geom);
console. log ( 'Area: ' + (area / 1e6 ). toFixed ( 2 ) + ' km²' );
});
</ script >
</ body >
</ html > content_copy
Copy
open_in_new
Open
import Map from 'ol/Map' ;
import View from 'ol/View' ;
import TileLayer from 'ol/layer/Tile' ;
import VectorLayer from 'ol/layer/Vector' ;
import XYZ from 'ol/source/XYZ' ;
import VectorSource from 'ol/source/Vector' ;
import Style from 'ol/style/Style' ;
import Fill from 'ol/style/Fill' ;
import Stroke from 'ol/style/Stroke' ;
import CircleStyle from 'ol/style/Circle' ;
import Draw from 'ol/interaction/Draw' ;
import { fromLonLat } from 'ol/proj' ;
import { getArea } from 'ol/sphere' ;
import 'ol/ol.css' ;
var source = new VectorSource ();
var drawLayer = new VectorLayer ({
source: source,
style: new Style ({
fill: new Fill ({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new Stroke ({ color: '#e63946' , width: 2 }),
image: new CircleStyle ({ radius: 6 , fill: new Fill ({ color: '#e63946' }) })
})
});
var map = new Map ({
target: 'map' ,
layers: [
new TileLayer ({ source: new XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new View ({ center: fromLonLat ([ 2.3522 , 48.8566 ]), zoom: 13 })
});
var draw = new Draw ({ source: source, type: 'Polygon' });
map. addInteraction (draw);
draw. on ( 'drawend' , function ( evt ) {
var geom = evt.feature. getGeometry ();
var area = getArea (geom);
console. log ( 'Area: ' + (area / 1e6 ). toFixed ( 2 ) + ' km²' );
});
var source = new ol.source.Vector();
var drawLayer = new ol.layer.Vector({
source: source,
style: new ol.style.Style({
fill: new ol.style.Fill({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new ol.style.Stroke({ color: '#e63946', width: 2 }),
image: new ol.style.Circle({ radius: 6, fill: new ol.style.Fill({ color: '#e63946' }) })
})
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 13 })
});
var draw = new ol.interaction.Draw({ source: source, type: 'Polygon' });
map.addInteraction(draw);
draw.on('drawend', function(evt) {
var geom = evt.feature.getGeometry();
var area = ol.sphere.getArea(geom);
console.log('Area: ' + (area / 1e6).toFixed(2) + ' km²');
}); <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draw Interaction in OpenLayers</title>
<link rel="stylesheet" href="https://unpkg.com/ol@10.11.0/ol.css">
<script src="https://unpkg.com/ol@10.11.0/dist/ol.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var source = new ol.source.Vector();
var drawLayer = new ol.layer.Vector({
source: source,
style: new ol.style.Style({
fill: new ol.style.Fill({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new ol.style.Stroke({ color: '#e63946', width: 2 }),
image: new ol.style.Circle({ radius: 6, fill: new ol.style.Fill({ color: '#e63946' }) })
})
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 13 })
});
var draw = new ol.interaction.Draw({ source: source, type: 'Polygon' });
map.addInteraction(draw);
draw.on('drawend', function(evt) {
var geom = evt.feature.getGeometry();
var area = ol.sphere.getArea(geom);
console.log('Area: ' + (area / 1e6).toFixed(2) + ' km²');
});
</script>
</body>
</html> import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import XYZ from 'ol/source/XYZ';
import VectorSource from 'ol/source/Vector';
import Style from 'ol/style/Style';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import CircleStyle from 'ol/style/Circle';
import Draw from 'ol/interaction/Draw';
import { fromLonLat } from 'ol/proj';
import { getArea } from 'ol/sphere';
import 'ol/ol.css';
var source = new VectorSource();
var drawLayer = new VectorLayer({
source: source,
style: new Style({
fill: new Fill({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new Stroke({ color: '#e63946', width: 2 }),
image: new CircleStyle({ radius: 6, fill: new Fill({ color: '#e63946' }) })
})
});
var map = new Map({
target: 'map',
layers: [
new TileLayer({ source: new XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new View({ center: fromLonLat([2.3522, 48.8566]), zoom: 13 })
});
var draw = new Draw({ source: source, type: 'Polygon' });
map.addInteraction(draw);
draw.on('drawend', function(evt) {
var geom = evt.feature.getGeometry();
var area = getArea(geom);
console.log('Area: ' + (area / 1e6).toFixed(2) + ' km²');
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draw Interaction in OpenLayers</title>
<link rel="stylesheet" href="https://unpkg.com/ol@10.11.0/ol.css">
<script src="https://unpkg.com/ol@10.11.0/dist/ol.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var source = new ol.source.Vector();
var drawLayer = new ol.layer.Vector({
source: source,
style: new ol.style.Style({
fill: new ol.style.Fill({ color: 'rgba(230, 57, 70, 0.2)' }),
stroke: new ol.style.Stroke({ color: '#e63946', width: 2 }),
image: new ol.style.Circle({ radius: 6, fill: new ol.style.Fill({ color: '#e63946' }) })
})
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=LKv9WPh4aJggV1CHU4Y8', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
drawLayer
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 13 })
});
var draw = new ol.interaction.Draw({ source: source, type: 'Polygon' });
map.addInteraction(draw);
draw.on('drawend', function(evt) {
var geom = evt.feature.getGeometry();
var area = ol.sphere.getArea(geom);
console.log('Area: ' + (area / 1e6).toFixed(2) + ' km²');
});
</script>
</body>
</html>