简体   繁体   中英

OpenLayers Remove Layer from map

I'm using a OpenLayers to add dots on the map from a search result. I can add them just fine, but I want to clear/remove the layer when the user does another search. I've tried using RemoveFeature() , using Destroy() , etc but everything I tried doesn't work.

What am I doing wrong?

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

The appropriate way to destroy features of a layer in OpenLayers 3 is to get the layer source, then clear the source:

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

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

Api Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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