如何为 Openlayers 切换器添加 KML 图层?

How to add the KML layer for Openlayers switcher?

如何通过 ol-ext Viglino 将 KML 层添加到切换器?

当我添加:

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

该图层未出现在地图上。

尝试:

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(),
  }),
});

要出现在图层切换器中,图层必须有标题属性

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

如果图层未出现在地图上,请检查是否在您的服务器 MIME 类型中启用了 .kml 扩展。