B
B
beduin012016-11-05 13:37:28
JavaScript
beduin01, 2016-11-05 13:37:28

Am I formatting the JSON object incorrectly?

var y = 
{
  "type":"Point","coordinates":[31.5,42.2],"bbox":[31.5,42.2,31.5,42.2]
}
L.geoJSON(y).addTo(Window.map);

This code works fine. But I should try to do:
var x = JSON.stringify(geojson);
L.geoJSON(x).addTo(Window.map);

how I get an error: "Invalid GeoJSON object".
However, if I output x to the browser console, then I will see the same content that I currently have in the y variable.
it all looks like this: e4c4beb397d64d32ae1198c77aba812d.png
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
_
_ _, 2016-11-05
@beduin01

JSON.strinfigy serializes a JavaScript object into a string. For example, to transfer somewhere.
geoJSON(x), in turn, wants to get just a JavaScript object as an argument, and not its string representation, which you palm off on it.
Therefore, the first option works for you, because y is not a JSON object, it is a JavaScript Object. It will become a JSON object only when you serialize it via JSON.stringify
Therefore, the first option works for you - you feed the JS object.
And the second one does not work - for some reason you turn the JS object into a string and feed the string already, although you need this geojson itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question