簡體   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