openlayers根据id添加/删除图片图层

按照id来删除

使用时调用方法即可

function clouds2d() {
    olMap.addLayer(new ol.layer.Image({
        source: new ol.source.ImageStatic({
            url: '../../public/img/12.png',
            imageExtent: extent,
            projection: "EPSG:3857",
            imageExtent: ol.proj.transformExtent(
                [
                    55,
                    -55.0,
                    155.0,
                    50.0
                ],
                "EPSG:4326",
                "EPSG:3857"
            ),
        }),
        id: 'myLayer'  // 设置图层的 ID
    }));
}

function deleteclouds2d() {
    var layer = olMap.getLayers().getArray().find(function(layer) {
        return layer.get('id') === 'myLayer';
    });
    if (layer) {
        olMap.removeLayer(layer);
    }
}

你可能感兴趣的:(开发语言,vue.js,前端,GIS,openlayers)