我正在使用 Leaflet 制作美国地图,并且具有单击功能来放大和更改州的颜色。这有效,但当单击另一个状态时,我无法让颜色返回到以前的颜色。目前,每次我单击某个状态时,颜色都会发生变化,并且不会删除之前的颜色变化。
这是我的代码:
var map = L.map('map').setView([37.8, -96], 4);
L.tileLayer('https://api.tiles.mapbox.com/v4/{z}/{x}/{y}.png?access_token=...', {
maxZoom: 8,
minZoom: 4,
attribution: ' ',
id: 'mapbox.light',
attribution: '<a target="_blank" href="https://www.mapbox.com/">Mapbox</a>'
}).addTo(map);
// get color depending on population density value
function getColor(d) {
return d < 1 ? '#173e34' : //green
'#e1cb7f'; //yellow
}
function style(feature) {
return {
weight: 2,
opacity: 1,
color: 'white',
dashArray: '',
fillOpacity: 1.9,
fillColor: getColor(feature.properties.availability)
};
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 1,
color: '#fff',
dashArray: …Run Code Online (Sandbox Code Playgroud)