async function loadPolygonToolActions( nbmap, draw ) {
nbmap.on('draw.create', updateSelectionArea);
nbmap.on('draw.delete', updateSelectionArea);
nbmap.on('draw.update', updateSelectionArea);
nbmap.on('draw.modechange', updateSelectionArea);
function updateSelectionArea(e) {
console.log(e);
let polygon = draw.getAll();
let mapPoints = [];
console.log(polygon);
mutable customerPoints = undefined;
if (nbmap.map.getLayer('customers')) {
nbmap.map.removeLayer('customers');
}
if (nbmap.map.getLayer('customers-cluster-count')) {
nbmap.map.removeLayer('customers-cluster-count');
}
if (nbmap.map.getLayer('unclustered-customer')) {
nbmap.map.removeLayer('unclustered-customer');
}
randomPoints.pointsArray.forEach(ptLocation => {
let turfPoint = turf.point([ptLocation.longitude,ptLocation.latitude]);
try {
if (turf.booleanPointInPolygon(turfPoint,turf.polygon(polygon.features[0].geometry.coordinates))) {
mapPoints.push(ptLocation);
}
} catch {
mapPoints.push(ptLocation);
}
});
let firstSymbolId;
const layers = nbmap.map.getStyle().layers;
for (const layer of layers) {
if (layer.type === 'symbol') {
firstSymbolId = layer.id;
break;
}
}
// remove points layer and then readd selected points based on geofence
let customersjson = {
type: 'FeatureCollection',
features: []
};
mapPoints.forEach( (pt, i) => {
let feature = {
type: 'Feature',
properties: {
business: pt.business,
name: pt.name,
phone: pt.phone
},
geometry: {
type: 'Point',
coordinates: [pt.longitude,pt.latitude]
}
}
customersjson.features.push(feature);
});
// passing these points out to a workbook cell
mutable customerPoints = customersjson;
if (!nbmap.map.getSource('customers')) {
nbmap.map.addSource('customers', {
type: 'geojson',
data: null,
cluster: true,
clusterRadius: 20,
clusterMaxZoom: 14
});
}
nbmap.map.getSource('customers').setData(customersjson);
if (nbmap.map.getLayer('customers')) {
nbmap.map.removeLayer('customers');
}
nbmap.map.addLayer({
type: 'circle',
source: 'customers',
id: 'customers',
filter: ['has', 'point_count'],
paint: {
'circle-color': [
'step',
['get', 'point_count'],
'#51bbd6',
100,
'#51bbd6',
750,
'#51bbd6'
],
'circle-radius': 5,
"circle-radius": ["case", ["get", "cluster"], 10, 5],
'circle-stroke-opacity': 0.4,
'circle-stroke-width': 1.5
}
}, firstSymbolId);
if (nbmap.map.getLayer('customers-cluster-count')) {
nbmap.map.removeLayer('customers-cluster-count');
}
nbmap.map.addLayer({
id:'customers-cluster-count',
type: 'symbol',
source: 'customers',
layout: {
'text-field': '{point_count_abbreviated}',
},
paint: {
'text-color': 'white'
}
});
if (nbmap.map.getLayer('unclustered-customer')) {
nbmap.map.removeLayer('unclustered-customer');
}
nbmap.map.addLayer({
id: 'unclustered-customer',
type: 'circle',
source: 'customers',
filter: ['!', ['has', 'point_count']],
paint: {
'circle-color': '#93CEA3',
'circle-radius': 10,
'circle-stroke-width': 1,
'circle-stroke-color': '#fff'
}
});
}
}