我正在使用OpenLayers。当选中复选框时,使用lat lng添加矢量图层。但是我试图使用相同的lat和lng删除该图层,但没有生效。
任何帮助将不胜感激。
var map;
var mapLat = 29.566;
var mapLng = -98.376;
var mapDefaultZoom = 17;
function initialize_map() {
map = new ol.Map({
target: "map",
layers: [
new ol.layer.Tile({
source: new ol.source.OSM({
url: "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png"
})
})
],
view: new ol.View({
center: ol.proj.fromLonLat([mapLng, mapLat]),
zoom: mapDefaultZoom
})
});
}
function add_map_point(action, lat, lng) {
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([parseFloat(lng), parseFloat(lat)], 'EPSG:4326', 'EPSG:3857')),
})]
}),
style: new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 0.5],
anchorXUnits: "fraction",
anchorYUnits: "fraction",
src: "https://img.icons8.com/officexs/16/000000/filled-flag.png"
})
})
});
if (action === 1) {
map.addLayer(vectorLayer);
} else {
map.removeLayer(vectorLayer);
}
}