Skip to content

Instantly share code, notes, and snippets.

@malwoodsantoro
Last active June 27, 2018 19:59
Show Gist options
  • Save malwoodsantoro/a16804a33d04ce6d9cdf81bc3d1c87df to your computer and use it in GitHub Desktop.
Save malwoodsantoro/a16804a33d04ce6d9cdf81bc3d1c87df to your computer and use it in GitHub Desktop.
Add pre-drawn draggable polygon
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title></title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.39.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v3.0.11/turf.min.js'></script>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.0/mapbox-gl-draw.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.0/mapbox-gl-draw.css' type='text/css'/>
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-79.820, 40.453],
zoom: 12
});
map.on('load', function(){
var draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});
map.addControl(draw);
var feature = {
id: 'unique-id',
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [
[
[
-79.83558654785156,
40.46523354237969
],
[
-79.82460021972656,
40.43727933676172
],
[
-79.78734970092772,
40.45961750937717
],
[
-79.80863571166992,
40.46614773587149
],
[
-79.83558654785156,
40.46523354237969
]
]
]
}
}
var featureIds = draw.add(feature);
console.log(featureIds);
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment