I have a Mapbox map and would like to get all the coordinates in an array of a circle that I custom drew using MapboxDraw.
This function takes the center of the circle, then by calculating the coordinates of the circle edges it decides which coordinates are in the circle and fills the array and then displayed via console log.
<script>
function CircleCoordinate(shape,shapeId) {
var Circlecoords = '';
var standard = 0.00001;
var center = shape.layer._latlng;
var centerPointlng = center.lng;
var centerPointlat = center.lat;
var latPoint = centerPointlat;
var lngPoint = centerPointlng;
var radius = shape.layer._mRadius;
for(var a = 0; a <= 360; a++)
{
var radiusPointlat = radius* Math.cos(a)+centerPointlat;
var radiusPointlng = radius* Math.sin(a)+centerPointlng;
for(var j = centerPointlng; j < radiusPointlng; j += standard)
{
// console.log("radiusPointlat????? ", radiusPointlat);
for(var i = centerPointlat;i < radiusPointlat; i += standard)
{
// console.log("IS ITTTTT????? ", isInside(centerPointlng, centerPointlat, radius, lngPoint, latPoint));
if (isInside(centerPointlng, centerPointlat, radius, lngPoint, latPoint))
{
Circlecoords += '('+latPoint + ',' + lngPoint + '),';
}
latPoint += standard;
}
lngPoint += standard;
}
}
</script>
Also take a look at the functions provided by http://turfjs.org/, which might be helpful, particularly http://turfjs.org/docs/#circle http://turfjs.org/docs/#pointGrid and http://turfjs.org/docs/#pointsWithinPolygon