gpt4 book ai didi

javascript - 如何删除传单图层并重新绘制?

转载 作者:行者123 更新时间:2023-11-30 08:28:15 28 4
gpt4 key购买 nike

我正在使用以下 JavaScript 代码在 map 中绘制多边形:

var map = L.map('map').setView([], 10);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=QA7i5Mpkd_m30IGElHziw', {
maxZoom: 18,
attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="http://mapbox.com">Mapbox</a>',
id: 'mapbox.light'
}).addTo(map);
map.doubleClickZoom.disable();

// get color depending on population density value
function getColor(d) {
return d > 3000 ? '#006400' :
d > 2500 ? '#FFEDA0' :
d > 2000 ? '#FFEDA0' :
d > 1500 ? '#c8ff58' :
d > 50 ? '#FFEDA0' :
d > 20 ? '#6fdc6f' :
d > 10 ? '#6fdc6f' :
'#FFEDA0';
}

function style(feature) {
return {
weight: 2,
opacity: 1,
color: '#999',
fillOpacity: 0.7,
fillColor: getColor(feature.properties.state1)
};
}

function highlightFeature(e) {
var layer = e.target;

layer.setStyle({
weight: 5,
color: '#666',
dashArray: '',
fillOpacity: 0.7
});

if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}

info.update(layer.feature.properties);
}

function resetHighlight(e) {
geojson.resetStyle(e.target);
info.update();
}

function zoomToFeature(e) {
map.fitBounds(e.target.getBounds());
map.doubleClickZoom.disable();
}

function searchText(e,feature) {
var layer = e.target;
var search = {
'zone': layer.feature.properties.name,
'zone_id':layer.feature.id
};
$.ajax({
type: "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url: "http:dataurl",
data: JSON.stringify(search), // Note it is important
success :function(result) {
// do what ever you want with data
// alert("suc");

},
error:function(error){
//alert("success");
}
});
}


var lastClickedLayer;

function onMapClick(e,feature) {
var layer = e.target;
$("#grid_name").html(layer.feature.properties.name);
set_zone(layer.feature.id);
searchText(e,feature);
}

function mapupdatecolor(startDate,endDate){

$.getJSON('http:dataurl', function(data) {
//console.log(data);
for (i = 0; i <80; i++) {
console.log("1 time state1 in console--"+campus['features'][i]['properties']['state1']);
campus['features'][i]['properties']['state1']=data[i].state1;
console.log("2 time state1 in console after change--"+campus['features'][i]['properties']['state1']);
}

map.removeLayer(L.geoJson);

var geojson = L.geoJson(campus, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);
});
}

function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
//click: zoomToFeature
click:onMapClick
});
}

geojson = L.geoJson(campus, {
style: style,
onEachFeature: onEachFeature
}).addTo(map);

var info = L.control();

info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this.update();
return this._div;
};

// method that we will use to update the control based on feature properties passed
info.update = function (props) {
this._div.innerHTML = '<h4><b>Zones<b></h4>' + (props ?
'<b>' + props.name + '</b><br />'
: 'Hover over a zone');
};

info.addTo(map);

campus是多边形坐标geoJson数据的变量。

这里我有函数mapupdatecolor,它在我点击一个按钮时更新多边形的颜色。但是现在不是更新当前图层的多边形中的颜色,而是在 map 上添加一个新图层,其中包含更新的多边形颜色。我正在使用 map.removeLayer(L.geoJson); 删除上一层,但它没有删除上一层。

最佳答案

将层的声明移到 mapupdatecolor 函数之外。

var geojsonLayer;

然后,在 mapupdatecolor 中,删除图层(如果存在)...

function mapupdatecolor() {
fetch('http://dataurl').then(response=>response.json()).then(jsonData=>{

if (geojsonLayer) { geojsonLayer.remove(); }

...然后用新获取的数据再次创建图层

        geojsonLayer = L.geoJson(jsonData, {style: style, onEachFeature: onEachFeature })
}
}

关于javascript - 如何删除传单图层并重新绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41799594/

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