Search code examples
cssangularopenlayersangular6openlayers-5

Cannot get the openlayers css in angular


I just started using openlayers 5 in my angular 6 and I follow this tutorial and also looking this SO question. My angular component right now has

import ol-map from 'ol/map';
import ol-xyz from 'ol/source/xyz';
import ol-tile from 'ol/layer/tile';
import ol-view from 'ol/View';
import * as ol-proj from 'ol/proj';

in my class

 olmap: ol-map;
 source: ol-xyz;
 layer: ol-tile;
 view: ol-view;

and then in my ngOnInit

this.source = new ol-xyz({
  url: 'https://api.tiles.mapbox.com/v4/mapbox.light/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw'
});

this.layer = new ol-tile({
  source: this.source
});

this.view = new ol-view({
  center: ol-proj.fromLonLat([6.661594, 50.433237]),
  zoom: 3,
});

this.olmap = new ol-map({
  target: 'map',
  layers: [this.layer],
  view: this.view
});

This works but I dont get the styling of the openlayers. I get the zoom buttons as simple, unstyled buttons, under the map div. What am I missing here? How can I insert the opelayers css style?

Thanks

enter image description here


Solution

  • You need to add the OpenLayers stylesheet to the styles sections of your angular.json file (this configuration was in .angular-cli.json prior to Angular 6).

    OpenLayers 5/6 Stylesheet
    node_modules/ol/ol.css

    OpenLayers 4 Stylesheet
    node_modules/openlayers/dist/ol.css

    angular.json

    {
      ...
      "projects": {
        "<project name>": {
          ...
          "architect": {
            "build": {
              ...
              "options": {
                ...
                "styles": [
                  "node_modules/ol/ol.css",
                  "src/styles.css"
                ],
                ...
              },
              ...
            },
            ...
            "test": {
              ...
              "options": {
                ...
                "styles": [
                  "node_modules/ol/ol.css",
                  "src/styles.css"
                ],
                ...
              }
            },
            ...
          }
        },
        ...
      },
      ...
    }