how to display a map as vector data from a url

Viewed 180

I am studying the workshop of OpenLayers as mentioned here in this link:

https://openlayers.org/workshop/en/vector/geojson.html

the code when run it should display a map as vector data according to the geojson file mentioned in the URL as shown in the code below.

the problem I am encountering now is, when I run the code, no map appears only the plus and minus buttons with dark background but the vector data does not appreciate.

to solve this issue, I attempted changing the formate of the file from geojson to be json as well as attempted to manipulate the path to the file as follows:

attempts:

I also adapted the code to be as follows:

url: './data/countries.geojson',
url: '/data/countries.geojson',
url: 'data/countries.geojson',

but non of them worked nor resulted in showing the map. please let me know how to render or display a map as vector data from a URL

main.js:

   import 'ol/ol.css';
import GeoJSON from 'ol/format/GeoJSON';
import Map from 'ol/Map';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import View from 'ol/View';
import {Fill, Stroke, Style, Text} from 'ol/style';

var style = new Style({
  fill: new Fill({
    color: 'rgba(255, 255, 255, 0.6)',
  }),
  stroke: new Stroke({
    color: '#319FD3',
    width: 1,
  }),
  text: new Text({
    font: '12px Calibri,sans-serif',
    fill: new Fill({
      color: '#000',
    }),
    stroke: new Stroke({
      color: '#fff',
      width: 3,
    }),
  }),
});

var vectorLayer = new VectorLayer({
  source: new VectorSource({
    url: 'data/countries.geojson',
    format: new GeoJSON(),
  }),
  style: function (feature) {
    style.getText().setText(feature.get('name'));
    return style;
  },
});

var map = new Map({
  layers: [vectorLayer],
  target: 'map-container',
  view: new View({
    center: [0, 0],
    zoom: 1,
  }),
});

index.html:

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>OpenLayers</title>
    <style>
      html, body, #map-container {
        margin: 0;
        height: 100%;
        width: 100%;
        font-family: sans-serif;
        background-color: #04041b;
      }
    </style>
  </head>
  <body>
    <div id="map-container"></div>
    <script src="main.js"></script>
  </body>
</html>
1 Answers

Probably you will see an error in your Browser developers console (Right click -> Inspect). Please post the error here and it will be easier to help you.

I suspect that one if these points will fix your issue:

  1. Check if countries.geojson is in your data folder. If not a 404 will apear in your console.

  2. Check if countries.geojson is valid geojson using a lint like https://geojson.io. If not your console says something about an invalid json format. It doesn't matter so if the extension is .json or .geojson.

  3. Be sure that you open the index.html from a local server (https protocol), opening it directly from folder (file protocol) won't load the file and would show a CORS error in your Javascript console.

Related