2

I have a select list where I can select which table and attributes I want to get printed on the leaflet map. Now when I select the first table it works and get printed to the leaflet map. But when I try to select the second table in the select list I get this syntax error:

JSON.parse: unexpected character at line 1 column 1 of the JSON data
"geometry": JSON.parse(d[fields.length]) //parse geometry  

This is my code:

function mapData(data){

    map.eachLayer(function(layer){
        if (typeof layer._url === "undefined"){
                map.removeLayer(layer);
        }
    });

    geojson = {
        "type": "FeatureCollection",
        "features": []
    };

    var dataArray = data.split(", ;");
    dataArray.pop();

    console.log(geojson);

    dataArray.forEach(function(d){
        d = d.split(", "); 

        var feature = {
            "type": "Feature",
            "properties": {}, //properties object container
            "geometry": JSON.parse(d[fields.length]) //parse geometry
        };      

        for (var i=0; i<fields.length; i++){
            feature.properties[fields[i]] = d[i];
        };

        geojson.features.push(feature);
    });
    window["mapDataLayer"] = L.geoJson(geojson).addTo(map);
};

And this is the response of the first succesvol table:

3, 3, McD, , 6.6668000000000, 53.218000000000, {"type":"Point","coordinates":[6.536668
,53.213000]}, ;

And this is the response om the second table that doesn't get printed to the leaflet map and throws the syntax error:

1400, 7, 19610, Centrum, 2413052, {"type":"MultiPolygon","coordinates":[[[[6.57886034312177,53.21809972266,41.2845400022343],[6.55981027443664,53.2275880222527,41.2689223056659],[6.5785471610371,53.2184343360823,41.2839346369728],[6.57886034312177,53.21809972266,41.2845400022343]]]]}, ;
L.fcg
  • 167
  • 1
  • 1
  • 10
  • It looks like you have added too many nested lists? – Timothy Dalton May 28 '16 at 17:41
  • 1
    On second glance, it looks correct, have you tried hard coding the object to see if it works w/o reading it from csv? Have you read this http://stackoverflow.com/questions/8524933/json-parse-unexpected-character-error ? – Timothy Dalton May 28 '16 at 17:53
  • I checked fields with `console.log(fields)` and I founf out that it adds the columns of second table to the fields array so that's why it's not working. But i dont't know how to fix it yet. This is how I'm pusing the values to fields: `data.forEach(function(field){ fieldList.push(field);` – L.fcg May 30 '16 at 10:07
  • Can you update your code to a minimum running example? I'll then be able to run it on my machine and help you fix the bug. – Timothy Dalton May 30 '16 at 20:12
  • I fixed it by adding this to my code: `fieldList.length = 0;` . Thanks for the help though! – L.fcg May 31 '16 at 09:28

0 Answers0