Activity Forums Salesforce® Discussions How to export all points within Leaflet polygon in Salesforce?

  • Prachi

    Member
    August 8, 2018 at 6:24 am

    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

  • shariq

    Member
    September 17, 2018 at 11:28 pm

    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.

Popular Salesforce Blogs

Popular Salesforce Videos