layer.getLatLngs() is null output

178 views
Skip to first unread message

ss

unread,
Oct 18, 2017, 1:49:30 PM10/18/17
to Leaflet
Hi,
   I am trying to save the polygon into database and later retrieve it back. I am trying the code snippet below.

 map.on('draw:created', function (e) {

    var type = e.layerType,
        layer = e.layer;
    
    if (type === 'polyline') {
        // structure the geojson object
        var geojson = {};
        geojson['type'] = 'Feature';
        geojson['geometry'] = {};
        geojson['geometry']['type'] = "Polyline";

        // 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));
          
    }
    else if (type === 'polygon') {
    drawnItems.addLayer(layer);
    layer.bindPopup('A popup!');
        // 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];
         layer.openPopup();
        // Finally, show the poly as a geojson object in the console
       console.log(JSON.stringify(geojson));
 
// console.log(down);
    }

    The output is always {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[null,null]]]}}
I actually need it to be POLYGON(( 175.28162956237796 -37.77007019461929,175.28506278991702 -37.77271618103962,175.27849674224854 -37.77458188377217,175.27781009674072 -37.77376776473304,175.28162956237796 -37.77007019461929 )) so I can save it into mysql.
How to restore later into leaftlet map with this output from mysql ?

Michelle Steele

unread,
Nov 1, 2017, 6:45:42 AM11/1/17
to Leaflet
You need to convert the geojson object to WKT. You could do this with a library such as GDAL or I just used wkt-parser. Here's another example http://bl.ocks.org/milkbread/6991519

shai singh

unread,
Nov 1, 2017, 10:59:11 PM11/1/17
to leafl...@googlegroups.com
Dear Michelle,
                        How about the reverse from WKT which I have saved into the database and back into json for me to view the geo fence?



--

---
You received this message because you are subscribed to a topic in the Google Groups "Leaflet" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/leaflet-js/732TtAQ-PwU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to leaflet-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michelle Steele

unread,
Nov 2, 2017, 6:06:10 AM11/2/17
to Leaflet
There are libraries that do both ways. e.g. https://github.com/mapbox/wellknown

Hope this helps
To unsubscribe from this group and all its topics, send an email to leaflet-js+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages