gpt4 book ai didi

javascript - OpenLayers 从 map 中删除图层

转载 作者:行者123 更新时间:2023-11-29 18:02:09 24 4
gpt4 key购买 nike

我正在使用 OpenLayers 根据搜索结果在 map 上添加点。我可以很好地添加它们,但我想在用户进行另一次搜索时清除/删除该层。我尝试过使用 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 Reference

关于javascript - OpenLayers 从 map 中删除图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34166575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com