How to add the KML layer for Openlayers switcher?

Viewed 283

how can i add a KML layer to the switcher by ol-ext Viglino?

When I add:

var vectorSource = new ol.layer.Vector({
  source: new ol.source.Vector({
  url: "IG1.kml",
  format: new ol.format.KML()
})

The layer does not appear on the map.

2 Answers

To appear in the layer switcher the layer must have a title property

var vectorSource = new ol.layer.Vector({
  title: "KML Layer",
  source: new ol.source.Vector({
    url: "IG1.kml",
    format: new ol.format.KML()
  })
})

If the layer is not appearing on the map check that .kml extension is enabled in your server MIME types.

Try :

import KML from 'ol/format/KML';
import VectorSource from 'ol/source/Vector';
import {Vector as VectorLayer} from 'ol/layer';

const vector = new VectorLayer({
  source: new VectorSource({
    url: 'IG1.kml',
    format: new KML(),
  }),
});
Related