繁体   English   中英

OpenLayers从地图上删除图层

[英]OpenLayers Remove Layer from map

我正在使用OpenLayers在搜索结果中在地图上添加点。 我可以很好地添加它们,但是我想在用户进行其他搜索时清除/删除该层。 我试过使用RemoveFeature()Destroy()等等,但是我尝试的所有方法都不起作用。

我究竟做错了什么?

http://jsfiddle.net/9Lzc1uu2/6/

        var USGSimagery = new ol.layer.Tile({
            myattribute: 'USGSimagery',
            source: new ol.source.TileWMS(({
                url: 'http://raster.nationalmap.gov/arcgis/services/Orthoimagery/USGS_EROS_Ortho_SCALE/ImageServer/WMSServer',
                params: {
                    'LAYERS': 0
                }
            }))
        });

        var view = new ol.View({
            //projection:projection
            center: ol.proj.transform(
                [-12934933.3971171, 5405304.89115131], 'EPSG:3857', 'EPSG:3857'),
            zoom: 18
        })


        var geolocStyle = new ol.style.Style({
            image: new ol.style.Icon(({
                anchor: [0.5, 46],
                anchorXUnits: 'fraction',
                anchorYUnits: 'pixels',
                opacity: 1,
                src: 'images/icon.png'
            }))
        });


        var map = new ol.Map({
            layers: [USGSimagery],
            loadTilesWhileInteracting: true,
            target: document.getElementById('map'),
            view: view
        });


        var searchResultsStyle = new ol.style.Style({
            image: new ol.style.Circle({
                radius: 6,
                fill: new ol.style.Fill({
                    color: '#3399CC'
                }),
                stroke: new ol.style.Stroke({
                    color: '#fff',
                    width: 2
                })
            })
        });

        var TestSearchResults = [{ 'Name': "R0045000030", 'X': "-12934933.3971171", 'Y': "5405304.89115131" },
        { 'Name': "R0238000050", 'X': "-12934887.0227854", 'Y': "5405285.39954225" },
        { 'Name': "R0310260660", 'X': "-12934830.2731638", 'Y': "5405249.69762986" }];

        var SearchDots = [];
        for (var i = 0; i < TestSearchResults.length; i++)
        {
            var item = TestSearchResults[i];

            var positionFeature = new ol.Feature({
                geometry: new ol.geom.Point([item["X"], item["Y"]]),
                name: item['Name']
            });
            positionFeature.setStyle(searchResultsStyle);

            SearchDots.push(positionFeature);
        }

        var featuresSearchResults = new ol.layer.Vector({
            map: map,
            source: new ol.source.Vector({
                features: SearchDots
            })
        });

        function DeleteResults()
        {
            // Delete Search Vectors from Map

            featuresSearchResults.destroy();
        }

在OpenLayers 3中销毁图层功能的适当方法是获取图层源,然后清除源:

    function DeleteResults()
    {
        // Delete Search Vectors from Map

        featuresSearchResults.getSource().clear();
    };

api参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM