1
votes

I am new to OpenLayers and its awesome. Easy to get running when you do the quick setup. But now I am using OpenLayers and Node.

I have been following the setup with npm for OpenLayers and added all code from example from here. https://openlayers.org/en/latest/doc/tutorials/bundle.html

The imports I am using are these:

import 'ol/ol.css';
import {Map, View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';

Now when I am gonna use a function to read a local GeoJson file using Node to build it I get this error:

"Unable to get property 'GeoJSON' of undefined or null reference"

I am running this function to read my file.

function getData() {
return new ol.layer.Vector({
source: new ol.source.Vector({
//url: 'http://www.yourserver.com/static/test.geojson',
url: 'test.geojson',
format: new ol.format.GeoJSON()
})
});
}

What do I need to import for the function to work, or how do I rewrite it to work with Node.? Is there any more documentation because I guess I don't get how it works 100% Thanks for help

1
If you are using imports you will also need to import VectorLayer, VectorSource and GeoJSON as in openlayers.org/en/latest/examples/vector-layer.html - Mike
@Mike Thanks for the answer. It now runs, but the more strange thing is that it can't load the geojson. I have checked the format and its fine. If I do this and not using Node it works. Really strange I must say. - mogren3000
Relative paths may be different in the new setup. Try using a full path such as http://localhost/your-path/test.geojson - Mike
I tried that and no luck. The error now saying " JSON.parse Error: Invalid character at position:1" but the geojson is valid. So I will go with the old-school way of building - mogren3000
Is the server set up with MIME type geojson? Especially with IIS that can be a problem. Try renaming .geojson to .json - Mike

1 Answers

0
votes

Answer is in comment section by @mike and @mahdi-mahmoodian 'Renaming the file and also move file to dist folder'