code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.42 , 50.08 ],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature' , geometry: { type: 'Polygon' , coordinates: [[]] } };
map. on ( 'load' , () => {
map. addSource ( 'draw' , { type: 'geojson' , data: geojson });
map. addLayer ({ id: 'draw-fill' , type: 'fill' , source: 'draw' , paint: { 'fill-color' : '#2196F3' , 'fill-opacity' : 0.2 } });
map. addLayer ({ id: 'draw-line' , type: 'line' , source: 'draw' , paint: { 'line-color' : '#2196F3' , 'line-width' : 2 } });
map. addSource ( 'vertices' , { type: 'geojson' , data: { type: 'FeatureCollection' , features: [] } });
map. addLayer ({ id: 'draw-points' , type: 'circle' , source: 'vertices' , paint: { 'circle-radius' : 5 , 'circle-color' : '#fff' , 'circle-stroke-width' : 2 , 'circle-stroke-color' : '#2196F3' } });
});
map. on ( 'click' , ( e ) => {
coords. push ([e.lngLat.lng, e.lngLat.lat]);
const ring = [ ... coords, coords[ 0 ]];
geojson.geometry.coordinates = [ring];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: coords. map ( c => ({ type: 'Feature' , geometry: { type: 'Point' , coordinates: c } })) });
});
document. getElementById ( 'finish-btn' ). addEventListener ( 'click' , () => {
if (coords. length < 3 ) return ;
map. off ( 'click' );
map. getCanvas ().style.cursor = '' ;
}); content_copy Copyopen_in_new Open
<! DOCTYPE html >
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" />
< link rel = "stylesheet" href = "https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< button id = "finish-btn" style = "position:absolute;top:10px;left:10px;z-index:1;padding:6px 14px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;font-weight:600;cursor:pointer;" >Finish Drawing</ button >
< script type = "module" >
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.42 , 50.08 ],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature' , geometry: { type: 'Polygon' , coordinates: [[]] } };
map. on ( 'load' , () => {
map. addSource ( 'draw' , { type: 'geojson' , data: geojson });
map. addLayer ({ id: 'draw-fill' , type: 'fill' , source: 'draw' , paint: { 'fill-color' : '#2196F3' , 'fill-opacity' : 0.2 } });
map. addLayer ({ id: 'draw-line' , type: 'line' , source: 'draw' , paint: { 'line-color' : '#2196F3' , 'line-width' : 2 } });
map. addSource ( 'vertices' , { type: 'geojson' , data: { type: 'FeatureCollection' , features: [] } });
map. addLayer ({ id: 'draw-points' , type: 'circle' , source: 'vertices' , paint: { 'circle-radius' : 5 , 'circle-color' : '#fff' , 'circle-stroke-width' : 2 , 'circle-stroke-color' : '#2196F3' } });
});
map. on ( 'click' , ( e ) => {
coords. push ([e.lngLat.lng, e.lngLat.lat]);
const ring = [ ... coords, coords[ 0 ]];
geojson.geometry.coordinates = [ring];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: coords. map ( c => ({ type: 'Feature' , geometry: { type: 'Point' , coordinates: c } })) });
});
document. getElementById ( 'finish-btn' ). addEventListener ( 'click' , () => {
if (coords. length < 3 ) return ;
map. off ( 'click' );
map. getCanvas ().style.cursor = '' ;
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import maplibregl from 'maplibre-gl' ;
import 'maplibre-gl/dist/maplibre-gl.css' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.42 , 50.08 ],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature' , geometry: { type: 'Polygon' , coordinates: [[]] } };
map. on ( 'load' , () => {
map. addSource ( 'draw' , { type: 'geojson' , data: geojson });
map. addLayer ({ id: 'draw-fill' , type: 'fill' , source: 'draw' , paint: { 'fill-color' : '#2196F3' , 'fill-opacity' : 0.2 } });
map. addLayer ({ id: 'draw-line' , type: 'line' , source: 'draw' , paint: { 'line-color' : '#2196F3' , 'line-width' : 2 } });
map. addSource ( 'vertices' , { type: 'geojson' , data: { type: 'FeatureCollection' , features: [] } });
map. addLayer ({ id: 'draw-points' , type: 'circle' , source: 'vertices' , paint: { 'circle-radius' : 5 , 'circle-color' : '#fff' , 'circle-stroke-width' : 2 , 'circle-stroke-color' : '#2196F3' } });
});
map. on ( 'click' , ( e ) => {
coords. push ([e.lngLat.lng, e.lngLat.lat]);
const ring = [ ... coords, coords[ 0 ]];
geojson.geometry.coordinates = [ring];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: coords. map ( c => ({ type: 'Feature' , geometry: { type: 'Point' , coordinates: c } })) });
});
document. getElementById ( 'finish-btn' ). addEventListener ( 'click' , () => {
if (coords. length < 3 ) return ;
map. off ( 'click' );
map. getCanvas ().style.cursor = '' ;
});
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[]] } };
map.on('load', () => {
map.addSource('draw', { type: 'geojson', data: geojson });
map.addLayer({ id: 'draw-fill', type: 'fill', source: 'draw', paint: { 'fill-color': '#2196F3', 'fill-opacity': 0.2 } });
map.addLayer({ id: 'draw-line', type: 'line', source: 'draw', paint: { 'line-color': '#2196F3', 'line-width': 2 } });
map.addSource('vertices', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
map.addLayer({ id: 'draw-points', type: 'circle', source: 'vertices', paint: { 'circle-radius': 5, 'circle-color': '#fff', 'circle-stroke-width': 2, 'circle-stroke-color': '#2196F3' } });
});
map.on('click', (e) => {
coords.push([e.lngLat.lng, e.lngLat.lat]);
const ring = [...coords, coords[0]];
geojson.geometry.coordinates = [ring];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: coords.map(c => ({ type: 'Feature', geometry: { type: 'Point', coordinates: c } })) });
});
document.getElementById('finish-btn').addEventListener('click', () => {
if (coords.length < 3) return;
map.off('click');
map.getCanvas().style.cursor = '';
}); <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<button id="finish-btn" style="position:absolute;top:10px;left:10px;z-index:1;padding:6px 14px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;font-weight:600;cursor:pointer;">Finish Drawing</button>
<script type="module">
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[]] } };
map.on('load', () => {
map.addSource('draw', { type: 'geojson', data: geojson });
map.addLayer({ id: 'draw-fill', type: 'fill', source: 'draw', paint: { 'fill-color': '#2196F3', 'fill-opacity': 0.2 } });
map.addLayer({ id: 'draw-line', type: 'line', source: 'draw', paint: { 'line-color': '#2196F3', 'line-width': 2 } });
map.addSource('vertices', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
map.addLayer({ id: 'draw-points', type: 'circle', source: 'vertices', paint: { 'circle-radius': 5, 'circle-color': '#fff', 'circle-stroke-width': 2, 'circle-stroke-color': '#2196F3' } });
});
map.on('click', (e) => {
coords.push([e.lngLat.lng, e.lngLat.lat]);
const ring = [...coords, coords[0]];
geojson.geometry.coordinates = [ring];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: coords.map(c => ({ type: 'Feature', geometry: { type: 'Point', coordinates: c } })) });
});
document.getElementById('finish-btn').addEventListener('click', () => {
if (coords.length < 3) return;
map.off('click');
map.getCanvas().style.cursor = '';
});
</script>
</body>
</html> import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[]] } };
map.on('load', () => {
map.addSource('draw', { type: 'geojson', data: geojson });
map.addLayer({ id: 'draw-fill', type: 'fill', source: 'draw', paint: { 'fill-color': '#2196F3', 'fill-opacity': 0.2 } });
map.addLayer({ id: 'draw-line', type: 'line', source: 'draw', paint: { 'line-color': '#2196F3', 'line-width': 2 } });
map.addSource('vertices', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
map.addLayer({ id: 'draw-points', type: 'circle', source: 'vertices', paint: { 'circle-radius': 5, 'circle-color': '#fff', 'circle-stroke-width': 2, 'circle-stroke-color': '#2196F3' } });
});
map.on('click', (e) => {
coords.push([e.lngLat.lng, e.lngLat.lat]);
const ring = [...coords, coords[0]];
geojson.geometry.coordinates = [ring];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: coords.map(c => ({ type: 'Feature', geometry: { type: 'Point', coordinates: c } })) });
});
document.getElementById('finish-btn').addEventListener('click', () => {
if (coords.length < 3) return;
map.off('click');
map.getCanvas().style.cursor = '';
});