code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY' ;
const map = new maptilersdk. Map ({
container: 'map' ,
style: maptilersdk.MapStyle. STREETS ,
center: [ 14.42 , 50.08 ],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature' , geometry: { type: 'Polygon' , coordinates: [[]] } };
function clickHandler ( e ) {
coords. push ([e.lngLat.lng, e.lngLat.lat]);
var ring = coords. concat ([coords[ 0 ]]);
geojson.geometry.coordinates = [ring];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: coords. map ( function ( c ) { return { type: 'Feature' , geometry: { type: 'Point' , coordinates: c } }; }) });
}
map. on ( 'load' , function () {
map. addSource ( 'draw' , { type: 'geojson' , data: geojson });
map. addLayer ({ id: 'draw-fill' , type: 'fill' , source: 'draw' , paint: { 'fill-color' : '#9C27B0' , 'fill-opacity' : 0.2 } });
map. addLayer ({ id: 'draw-line' , type: 'line' , source: 'draw' , paint: { 'line-color' : '#9C27B0' , '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' : '#9C27B0' } });
map. on ( 'click' , clickHandler);
});
document. getElementById ( 'finish-btn' ). addEventListener ( 'click' , function () {
if (coords. length < 3 ) return ;
map. off ( 'click' , clickHandler);
map. getCanvas ().style.cursor = '' ;
});
document. getElementById ( 'clear-btn' ). addEventListener ( 'click' , function () {
coords. length = 0 ;
geojson.geometry.coordinates = [[]];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: [] });
map. off ( 'click' , clickHandler);
map. on ( 'click' , clickHandler);
map. getCanvas ().style.cursor = 'crosshair' ;
}); 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://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css" />
< script src = "https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.umd.min.js" ></ script >
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< div style = "position:absolute;top:10px;left:10px;z-index:1;display:flex;gap:6px;" >
< button id = "finish-btn" style = "padding:6px 14px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;font-weight:600;cursor:pointer;" >Finish</ button >
< button id = "clear-btn" style = "padding:6px 14px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;font-weight:600;cursor:pointer;" >Clear</ button >
</ div >
< script >
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY' ;
const map = new maptilersdk. Map ({
container: 'map' ,
style: maptilersdk.MapStyle. STREETS ,
center: [ 14.42 , 50.08 ],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature' , geometry: { type: 'Polygon' , coordinates: [[]] } };
function clickHandler ( e ) {
coords. push ([e.lngLat.lng, e.lngLat.lat]);
var ring = coords. concat ([coords[ 0 ]]);
geojson.geometry.coordinates = [ring];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: coords. map ( function ( c ) { return { type: 'Feature' , geometry: { type: 'Point' , coordinates: c } }; }) });
}
map. on ( 'load' , function () {
map. addSource ( 'draw' , { type: 'geojson' , data: geojson });
map. addLayer ({ id: 'draw-fill' , type: 'fill' , source: 'draw' , paint: { 'fill-color' : '#9C27B0' , 'fill-opacity' : 0.2 } });
map. addLayer ({ id: 'draw-line' , type: 'line' , source: 'draw' , paint: { 'line-color' : '#9C27B0' , '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' : '#9C27B0' } });
map. on ( 'click' , clickHandler);
});
document. getElementById ( 'finish-btn' ). addEventListener ( 'click' , function () {
if (coords. length < 3 ) return ;
map. off ( 'click' , clickHandler);
map. getCanvas ().style.cursor = '' ;
});
document. getElementById ( 'clear-btn' ). addEventListener ( 'click' , function () {
coords. length = 0 ;
geojson.geometry.coordinates = [[]];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: [] });
map. off ( 'click' , clickHandler);
map. on ( 'click' , clickHandler);
map. getCanvas ().style.cursor = 'crosshair' ;
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import * as maptilersdk from '@maptiler/sdk' ;
import '@maptiler/sdk/dist/maptiler-sdk.css' ;
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY' ;
const map = new maptilersdk. Map ({
container: 'map' ,
style: maptilersdk.MapStyle. STREETS ,
center: [ 14.42 , 50.08 ],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature' , geometry: { type: 'Polygon' , coordinates: [[]] } };
function clickHandler ( e ) {
coords. push ([e.lngLat.lng, e.lngLat.lat]);
var ring = coords. concat ([coords[ 0 ]]);
geojson.geometry.coordinates = [ring];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: coords. map ( function ( c ) { return { type: 'Feature' , geometry: { type: 'Point' , coordinates: c } }; }) });
}
map. on ( 'load' , function () {
map. addSource ( 'draw' , { type: 'geojson' , data: geojson });
map. addLayer ({ id: 'draw-fill' , type: 'fill' , source: 'draw' , paint: { 'fill-color' : '#9C27B0' , 'fill-opacity' : 0.2 } });
map. addLayer ({ id: 'draw-line' , type: 'line' , source: 'draw' , paint: { 'line-color' : '#9C27B0' , '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' : '#9C27B0' } });
map. on ( 'click' , clickHandler);
});
document. getElementById ( 'finish-btn' ). addEventListener ( 'click' , function () {
if (coords. length < 3 ) return ;
map. off ( 'click' , clickHandler);
map. getCanvas ().style.cursor = '' ;
});
document. getElementById ( 'clear-btn' ). addEventListener ( 'click' , function () {
coords. length = 0 ;
geojson.geometry.coordinates = [[]];
map. getSource ( 'draw' ). setData ( JSON . parse ( JSON . stringify (geojson)));
map. getSource ( 'vertices' ). setData ({ type: 'FeatureCollection' , features: [] });
map. off ( 'click' , clickHandler);
map. on ( 'click' , clickHandler);
map. getCanvas ().style.cursor = 'crosshair' ;
});
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
container: 'map',
style: maptilersdk.MapStyle.STREETS,
center: [14.42, 50.08],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[]] } };
function clickHandler(e) {
coords.push([e.lngLat.lng, e.lngLat.lat]);
var ring = coords.concat([coords[0]]);
geojson.geometry.coordinates = [ring];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: coords.map(function(c) { return { type: 'Feature', geometry: { type: 'Point', coordinates: c } }; }) });
}
map.on('load', function() {
map.addSource('draw', { type: 'geojson', data: geojson });
map.addLayer({ id: 'draw-fill', type: 'fill', source: 'draw', paint: { 'fill-color': '#9C27B0', 'fill-opacity': 0.2 } });
map.addLayer({ id: 'draw-line', type: 'line', source: 'draw', paint: { 'line-color': '#9C27B0', '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': '#9C27B0' } });
map.on('click', clickHandler);
});
document.getElementById('finish-btn').addEventListener('click', function() {
if (coords.length < 3) return;
map.off('click', clickHandler);
map.getCanvas().style.cursor = '';
});
document.getElementById('clear-btn').addEventListener('click', function() {
coords.length = 0;
geojson.geometry.coordinates = [[]];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: [] });
map.off('click', clickHandler);
map.on('click', clickHandler);
map.getCanvas().style.cursor = 'crosshair';
}); <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.css" />
<script src="https://cdn.maptiler.com/maptiler-sdk-js/v4.1.0/maptiler-sdk.umd.min.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<div style="position:absolute;top:10px;left:10px;z-index:1;display:flex;gap:6px;">
<button id="finish-btn" style="padding:6px 14px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;font-weight:600;cursor:pointer;">Finish</button>
<button id="clear-btn" style="padding:6px 14px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;font-weight:600;cursor:pointer;">Clear</button>
</div>
<script>
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
container: 'map',
style: maptilersdk.MapStyle.STREETS,
center: [14.42, 50.08],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[]] } };
function clickHandler(e) {
coords.push([e.lngLat.lng, e.lngLat.lat]);
var ring = coords.concat([coords[0]]);
geojson.geometry.coordinates = [ring];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: coords.map(function(c) { return { type: 'Feature', geometry: { type: 'Point', coordinates: c } }; }) });
}
map.on('load', function() {
map.addSource('draw', { type: 'geojson', data: geojson });
map.addLayer({ id: 'draw-fill', type: 'fill', source: 'draw', paint: { 'fill-color': '#9C27B0', 'fill-opacity': 0.2 } });
map.addLayer({ id: 'draw-line', type: 'line', source: 'draw', paint: { 'line-color': '#9C27B0', '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': '#9C27B0' } });
map.on('click', clickHandler);
});
document.getElementById('finish-btn').addEventListener('click', function() {
if (coords.length < 3) return;
map.off('click', clickHandler);
map.getCanvas().style.cursor = '';
});
document.getElementById('clear-btn').addEventListener('click', function() {
coords.length = 0;
geojson.geometry.coordinates = [[]];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: [] });
map.off('click', clickHandler);
map.on('click', clickHandler);
map.getCanvas().style.cursor = 'crosshair';
});
</script>
</body>
</html> import * as maptilersdk from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
container: 'map',
style: maptilersdk.MapStyle.STREETS,
center: [14.42, 50.08],
zoom: 13
});
const coords = [];
const geojson = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [[]] } };
function clickHandler(e) {
coords.push([e.lngLat.lng, e.lngLat.lat]);
var ring = coords.concat([coords[0]]);
geojson.geometry.coordinates = [ring];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: coords.map(function(c) { return { type: 'Feature', geometry: { type: 'Point', coordinates: c } }; }) });
}
map.on('load', function() {
map.addSource('draw', { type: 'geojson', data: geojson });
map.addLayer({ id: 'draw-fill', type: 'fill', source: 'draw', paint: { 'fill-color': '#9C27B0', 'fill-opacity': 0.2 } });
map.addLayer({ id: 'draw-line', type: 'line', source: 'draw', paint: { 'line-color': '#9C27B0', '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': '#9C27B0' } });
map.on('click', clickHandler);
});
document.getElementById('finish-btn').addEventListener('click', function() {
if (coords.length < 3) return;
map.off('click', clickHandler);
map.getCanvas().style.cursor = '';
});
document.getElementById('clear-btn').addEventListener('click', function() {
coords.length = 0;
geojson.geometry.coordinates = [[]];
map.getSource('draw').setData(JSON.parse(JSON.stringify(geojson)));
map.getSource('vertices').setData({ type: 'FeatureCollection', features: [] });
map.off('click', clickHandler);
map.on('click', clickHandler);
map.getCanvas().style.cursor = 'crosshair';
});