Activity › Forums › Salesforce® Discussions › How to export all points within Leaflet polygon in Salesforce?
Tagged: Code Sample, Console Log, Export, GeoJSON Object, Leaflet Polygon
-
How to export all points within Leaflet polygon in Salesforce?
Posted by madhulika shah on August 7, 2018 at 1:47 PMHow to export all points within Leaflet polygon in Salesforce?
shariq replied 7 years, 8 months ago 3 Members · 2 Replies -
2 Replies
-
Hello Madhulika,
Although ‘export’ is very ambiguous, here’s a way to console log the drawn polygon as ‘geojson’ string:-
map.on(‘draw:created’, function (e) { var type = e.layerType, layer = e.layer; if (type === ‘polygon’) { // structure the geojson object var geojson = {}; geojson[‘type’] = ‘Feature’; geojson[‘geometry’] = {}; geojson[‘geometry’][‘type’] = “Polygon”; // export the coordinates from the layer coordinates = []; latlngs = layer.getLatLngs(); for (var i = 0; i < latlngs.length; i++) { coordinates.push([latlngs[i].lng, latlngs[i].lat]) } // push the coordinates to the json geometry geojson[‘geometry’][‘coordinates’] = [coordinates]; // Finally, show the poly as a geojson object in the console console.log(JSON.stringify(geojson)); } drawnItems.addLayer(layer); });I hope this will help.
thanks
- [adinserter block='9']
-
Hi,
I have found this code online, try this and let me know –
map.on(‘draw:created’, function (e) { var type = e.layerType, layer = e.layer; if (type === ‘polygon’) { // here you got the polygon points var points = layer._latlngs; // here you can get it in geojson format var geojson = layer.toGeoJSON(); } // here you add it to a layer to display it in the map drawnItems.addLayer(layer); });Hope this helps.
Log In to reply.