-
-
Save ThomasG77/8b03671e46613c11a63d8825376c5c33 to your computer and use it in GitHub Desktop.
Points, points & points - MapboxGL JS version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title>Points, points & points - MapboxGL JS version</title> | |
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.41.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.41.0/mapbox-gl.css' rel='stylesheet' /> | |
<style> | |
body { | |
margin:0; | |
padding:0; | |
} | |
#map { | |
position:absolute; | |
top:0; | |
bottom:0; | |
width:100%; | |
} | |
#text { | |
position: absolute; | |
z-index: 1; | |
bottom: 1em; | |
left: 1em; | |
padding: 15px; | |
background-color: white; | |
border: #0e0e0e 1px solid; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='map'> | |
<div id="text"> | |
<h1 id="title">How Many Points...</h1> | |
<div id="docs"> | |
<p> | |
Number of points to place on the map: | |
<input id="num" value="1000" type="number"> | |
<button id="draw">Draw</button> | |
<button id="clear">Clear</button> | |
</p> | |
<p> | |
This page is an <b>MapboxGL JS</b> demo port of this <a href="https://derekswingley.com/lab/olpts/">OpenLayers 2 Derek Swingley demo</a> | |
</p> | |
</div> | |
</div> | |
</div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoidGhvbWFzZzc3IiwiYSI6Im5Yc3p3SW8ifQ.FtzS4C8q_UJpOhrVnJfQfg'; | |
var map = new mapboxgl.Map({ | |
container: 'map', // container id | |
style: 'mapbox://styles/mapbox/streets-v9', // stylesheet location | |
center: [-74.50, 40], // starting position [lng, lat] | |
zoom: 9 // starting zoom | |
}); | |
map.addControl(new mapboxgl.NavigationControl()); | |
var bounds = map.getBounds().toArray(); | |
var flattenedBounds = [...bounds[0], ...bounds[1]]; | |
map.on('style.load', function() { | |
map.addSource('sample', { | |
type: 'geojson', | |
data: turf.random.randomPoint(Number(document.getElementById('num').value), { | |
bbox: flattenedBounds | |
}) | |
}); | |
map.addLayer({ | |
"id": "point", | |
"source": "sample", | |
"type": "circle", | |
"paint": { | |
"circle-radius": 6, | |
"circle-color": "#3399CC", | |
"circle-stroke-width": 2, | |
"circle-stroke-color": "#fff" | |
} | |
}); | |
}); | |
document.getElementById('draw').addEventListener('click', function(e) { | |
var b = map.getBounds().toArray(); | |
var flattenedB = [...bounds[0], ...bounds[1]]; | |
var points = turf.random.randomPoint(Number(document.getElementById('num').value), { | |
bbox: flattenedB | |
}); | |
map.getSource('sample').setData(points); | |
}); | |
document.getElementById('clear').addEventListener('click', function(e) { | |
map.getSource('sample').setData({ | |
"type": "FeatureCollection", | |
"features": [] | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment