2

I try to display multi line in the map but it return

"Error: Invalid GeoJSON object"

Here is my code

        $.ajax({
            type:     "POST",
            url:      "server/drawPipe.php",
            data:     {pwa_code : pwa_code},
            success: function(data){

                data = JSON.stringify(data);

                var jsonLayer = L.geoJson().addTo(map_google);
                jsonLayer.addData(data);

            }
        });

Here is the result that returns from drawPipe.php (I validate this Geojson via http://geojsonlint.com/, the result is correct)

{"type":"MultiLineString","coordinates":[[[101.146594802185,17.2898164508615],[101.146539827898,17.2895967362354],[101.146516345161,17.2894192118485],[101.146497648309,17.2893130415973],[101.14649430089,17.2892767147386],[101.146484934618,17.2892244862197],[101.146463647515,17.2891385623201],[101.146586602643,17.288998402757],[101.146623647723,17.288956177505],[101.146737097675,17.2888484892937],[101.146799533689,17.2887918684111],[101.146903317291,17.2886909399864],[101.146909938481,17.288506958459],[101.14690397303,17.2880796318353],[101.146901612993,17.2880256437337],[101.146881556,17.2875721572498],[101.146878504378,17.2874770222705],[101.146882907032,17.2873846465749],[101.146894138478,17.2871466305424],[101.146894140357,17.2871465403733],[101.146894143724,17.2871426616135],[101.146894464609,17.2867666924102],[101.146907865134,17.2864394148899],[101.146914075563,17.2862862756153],[101.14694128003,17.2857201294004],[101.14695643629,17.2852044468443],[101.14696340772,17.2848288760523],[101.146963060262,17.2847935089181],[101.146962287802,17.2847041908791],[101.146944918202,17.2845491549589],[101.146925868417,17.2844822172579],[101.146905152812,17.2844097450123],[101.146850132731,17.2842740108223],[101.146804593425,17.2841998886639],[101.146740705649,17.2841181071168],[101.146671904821,17.2840491304472],[101.146593174702,17.2839772567031],[101.146481452044,17.2838906765891],[101.146409944079,17.2838401398767],[101.146313904738,17.2837664899673],[101.14618180933,17.2836839380681]]]}

Could anyone suggest me please

4
  • Try not using JSON.stringify, just take that line out. Commented May 23, 2016 at 7:19
  • still get the same error
    – pich_chaya
    Commented May 23, 2016 at 7:27
  • OK so second idea, do you know why the server returns a MultiLineString when the line has only one part? If you use a LineString (and take out one pair of square brackets), the geometry is still valid. Possibly that's an issue. Commented May 23, 2016 at 7:37
  • Why do you return your coordinates nested on the third level of an array? coordinates[[ [x y],[x y] ]]? Try removing one nesting level.
    – til_b
    Commented May 23, 2016 at 7:37

1 Answer 1

1

The GeoJSON is valid and renders fine, see http://playground-leaflet.rhcloud.com/kogi/edit?html,output.

However, the use of jquery to fetch it is not. Make sure the request is returning JSON (hint: use dataType: 'json' in your jquery ajax call), and do not stringify the result Ion the contrary, run JSON.parse() if all you've got is a string). If you console.log() the geojson, it should look like a javascript object, not like a string.

Alernatively, ditch jQuery and use the fetch API magical JSON-parsing capabilities.

1
  • I found the answer!! Only add dataType : 'json' to ajax $.ajax({ type: "POST", url: "server/drawPipe.php", data: {pwa_code : pwa_code}, dataType : 'json', success: function(data){ L.geoJson(data).addTo(map_google); } });
    – pich_chaya
    Commented May 23, 2016 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.