From 561f9a81645189eafe4ac0cb97c2f00ff53316bd Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Sat, 13 Oct 2018 10:33:23 +0200 Subject: [PATCH] carto: add drawLayer method --- app/javascript/shared/carto.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/javascript/shared/carto.js b/app/javascript/shared/carto.js index 52aa1e579..ff76f6e86 100644 --- a/app/javascript/shared/carto.js +++ b/app/javascript/shared/carto.js @@ -6,6 +6,20 @@ import L from 'leaflet'; export { DEFAULT_POSITION, LAT, LON }; const LAYERS = {}; +export function drawLayer(map, data, style, layerName = 'default') { + removeLayer(map, layerName); + + if (Array.isArray(data) && data.length > 0) { + const layer = createLayer(map, layerName); + + data.forEach(function(item) { + layer.addData(item.geometry); + }); + + layer.setStyle(style).addTo(map); + } +} + function createLayer(map, layerName) { const layer = (LAYERS[layerName] = new L.GeoJSON(undefined, { interactive: false