Draw Circle on LineString
by amay077
HTML
<script src="https://api.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v1.6.1/mapbox-gl.css">
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
<div id="map" style="position: absolute; top: 0; bottom: 0; width: 100%;"></div>
CSS
body { margin: 0; padding: 0; }
JavaScript
const routeSource = {
'type': 'geojson',
'data': {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
[-122.48369693756104, 37.83381888486939],
[-122.48356819152832, 37.82954808664175],
[-122.48751640319824, 37.83168351665737],
[-122.49223709106445, 37.83337825839438],
[-122.49378204345702, 37.83368330777276]
]
}
}
};
const pointFeature = {
type: 'FeatureCollection',
features: [
{
"type": "Feature",
"properties": {},
"geometry": {
"type":"Point",
"coordinates": []
}
}
]
};
const map = new mapboxgl.Map({
container: 'map',
center: [-122.486052, 37.830348],
zoom: 15,
style: {
version: 8,
sources: {
OSM: {
type: "raster",
tiles: [
"https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
],
tileSize: 256,
attribution:
"OpenStreetMap",
},
},
layers: [{
id: "BASEMAP",
type: "raster",
source: "OSM",
minzoom: 0,
maxzoom: 18,
}],
},
});
map.once('load', () => {
map.addSource('route', routeSource);
map.addLayer({
'id': 'route',
'type': 'line',
'source': 'route',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#888',
'line-width': 8
}
});
map.addSource('pointOnLine', {
'type': 'geojson',
'data': pointFeature
});
pointSource = map.getSource('pointOnLine');
map.addLayer({
'id': 'pointOnLine',
'source': 'pointOnLine',
'type': 'circle',
'paint': {
'circle-radius': 10,
'circle-color': "red",
'circle-opacity': 0.7,
}
});
map.on('mousemove', e => {
const start = routeSource.data.geometry.coordinates[0];
const nearest = turf.nearestPointOnLine(routeSource.data,
[e.lngLat.lng, e.lngLat.lat], { units:...