[英]Openlayers: How can I remove a feature?
當用戶單擊地圖時,我試圖在Openlayers上運行腳本,添加一個指針或標記。 我已經完成了這一部分,但是這里有一個小問題。
當用戶單擊地圖時,較舊的指針將在那里鋼化,但是我想刪除所有指針,並嘗試僅保留用戶最近單擊的點。
請幫我解決這個問題。 這是我的代碼:[openlayers 3]
<script>
var
vectorSource = new ol.source.Vector(),
vectorLayer = new ol.layer.Vector({
source: vectorSource
}),
olView = new ol.View({
center: ol.proj.fromLonLat([48.4831, 36.6681]),
zoom: 8,
minZoom: 2,
maxZoom: 20
}),
map = new ol.Map({
target: document.getElementById('map'),
view: olView,
layers: [
new ol.layer.Tile({
style: 'Aerial',
source: new ol.source.OSM()
}),
vectorLayer
]
})
;
var iconStyle = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'https://openlayers.org/en/v3.8.2/examples/data/icon.png'
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
fill: new ol.style.Fill({ color: '#000' }),
stroke: new ol.style.Stroke({
color: '#fff', width: 2
}),
text: 'Some text'
})
});
map.on('click', function(evt){
var feature = new ol.Feature(
new ol.geom.Point(evt.coordinate)
);
var lon = ol.proj.toLonLat(evt.coordinate)[0];
var lat = ol.proj.toLonLat(evt.coordinate)[1];
console.info('longitude is: ' + lon + 'latitude is: ' + lat);
feature.setStyle(iconStyle);
vectorSource.addFeature(feature);
});
</script>
使用vectorSource.clear();
在添加功能之前:
vectorSource.clear();
vectorSource.addFeature(feature);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.