Amir
Amir

Reputation: 685

How to solve "Uncaught SyntaxError: Unexpected token <" error

I am working on openlayers 3 and I am trying to execute this simple JavaScript function:

function mapScript() {
    // create a vector source that loads a GeoJSON file
    var vectorSource = new ol.source.Vector({
        projection: 'EPSG:3857',
        format: new ol.format.GeoJSON(),
        url: 'countries.geojson'
    });

    // a vector layer to render the source
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource
    });

    var center = ol.proj.transform([0, 0], 'EPSG:4326', 'EPSG:3857');

    var view = new ol.View({
        center: center,
        zoom: 1
    });

    // the vector layer gets added like a raster layer
    var map = new ol.Map({
        target: 'map',
        layers: [vectorLayer],
        view: view
    });
} // END of mapScript()

I have seen many similar questions, but the solutions are not working for my case. I am new to openlayers so kindly anyone guide me to solve this issue.

Thanks in advance for your time guys.

UPDATE:

My browser's "Network" bar shows me this:

enter image description here

The source of "countries.geojson" is this:

https://github.com/openlayers/ol3/blob/master/examples/data/geojson/countries.geojson

I downloaded this file, and now its in my local directory. I think this linking of file in my project is causing problem. Can any body help me in using this file appropriately?

Upvotes: 1

Views: 1706

Answers (1)

Alvin Lindstam
Alvin Lindstam

Reputation: 3142

https://github.com/openlayers/ol3/blob/master/examples/data/geojson/countries.geojson is not a GeoJSON document, it's a HTML page on github about the GeoJSON document in the repo. The actual GeoJSON data can be found by clicking the "Raw" link on that page: https://raw.githubusercontent.com/openlayers/ol3/master/examples/data/geojson/countries.geojson

Upvotes: 2

Related Questions