0

I want to load a local geojson to my map using openlayers.

Here is what I currently have:

var gjsonFile = new ol.layer.Vector({
source: new ol.source.Vector({
format: new ol.format.GeoJSON({
}),
url: './data/geojson/plan.geojson'
})
});

map.addLayer(gjsonFile);

But nothing is displayed, I get this error:

Uncaught SyntaxError: Unexpected token < in JSON at position 2

Anyone have a solution?

NB. i tried with this url: https://c.data.osmbuildings.org/0.2/anonymous/tile/15/17607/10743.json and it worked, so i have a problem with local paths.

Raduan Santos
  • 1,023
  • 1
  • 21
  • 45
Meryem
  • 63
  • 6
  • 1
    Maybe the error is in the content of your local json, and not due to the path. What happens if you download a copy of 10743.json then copy it to your local path and use `url: './data/geojson/10743.json'` ? – Mike Jan 04 '19 at 19:37
  • Yes, i've already do it, but i get the same error mentioned above in my question. – Meryem Jan 07 '19 at 11:14
  • 1
    Taking a copy of 10743.json onto my localhost and then reading it works for me so perhaps you have a MIME type configuration problem on your server. Can you access your data directly from the browser address bar? e.g.`http://yourdomain/data/geojson/plan.geojson` – Mike Jan 07 '19 at 11:40
  • Yes i think that''s the problem, because i can't access to my data by the browser. – Meryem Jan 07 '19 at 14:10
  • thank you for your answer, have u any proposition to resolve it? – Meryem Jan 07 '19 at 14:36
  • 1
    If you are using Windows localhost go to computer managerment, services and applications, IIS then select MIME types from the options and add an extry for extension `.geojson` type `application/json` Other server types I've used haven't need any special setup. – Mike Jan 07 '19 at 14:59
  • thank you ^^ i will try it, i just not find the IIS in my computer management :\ – Meryem Jan 07 '19 at 16:40

1 Answers1

0

You can use require to read a json file from your localhost directory and use (new GeoJSON()).readFeatures for reading the json object. Ref the answer here https://stackoverflow.com/a/61948057/13594097

Venkat
  • 1