我将geoJson地图区域渲染到具有初始不透明度的地图上。我有一个滑块可以在飞行中改变这种不透明度。
下面是我在自定义leaflet控件中对输入更改事件执行的动态设置不透明度(typescript)的位置:
this.layers.forEach((r: L.GeoJSON<any>) => {
r.eachLayer((layer: any) => {
layer.setStyle({ fillOpacity: vm.mapOptions.heatmapOpacity });
});
});
setTimeout(() => this.map.invalidateSize());
我还可以将鼠标悬停在区域上,在这种情况下,当区域悬停时,我会降低不透明度并在活动区域上放置一个边框。
当它们离开该区域时,它当前在该区域上使用resetStyles将其重置为以前的样式。我在options onFeature回调中设置了这一点。该区域在mouseover事件中突出显示,并在mouseout事件中重置,如下所示。
options = {
style: { stroke: false,
fillColor: regionColor,
fillOpacity: mapOptions.heatmapOpacity },
onEachFeature: (feature, layer) => {
layer.on({
mouseover: (e)=> {
const lr = e.target;
lr.setStyle({
weight: 5,
color: "#666",
dashArray: "",
fillOpacity: 0.7,
stroke: true
});
if (!L.Browser.ie && !L.Browser.opera12 && !L.Browser.edge) {
lr.bringToFront();
}
},
mouseout: (e) => {
prop.featureGeoJson.resetStyle(e.target);
}
});
}
};
问题是,如果我使用setStyle将不透明度设置为不同的值,那么我进入一个区域,然后再次离开该区域,调用resetStyle会在对不透明度进行更改之前将样式重置回原始默认样式。
是否可以在层上设置默认样式,以便调用resetStyle将样式设置为具有新不透明度的我的值,而不是设置为创建区域时设置的原始不透明度?我该怎么做呢?
发布于 2017-12-05 07:35:20
Leaflet GeoJSON图层组的resetStyle
方法将重新应用在创建该组时应用的样式,或默认样式(如果未提供样式):
将给定向量层的样式重置为原始GeoJSON样式,这对于在悬停事件后重置样式非常有用。
如果稍后更改该组中一个或所有矢量图层的样式,则在使用resetStyle
时将覆盖该样式,并将应用初始样式。
一个简单的解决方法是简单地修改GeoJSON图层组的style
选项以及。但是,它将影响其所有子层:
group.options.style = newStyle;
(这是@GabyakaGPetrioli的答案中建议的,但您必须将其应用于组,而不是单个功能)
另一种解决方案是记录每个矢量层的新样式,并在想要恢复以前的状态时使用该记录值,而不是使用组的resetStyle
方法。
var map = L.map("map").setView([48.85, 2.35], 12);
var geojson = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [2.35, 48.85]
}
};
var point;
var startStyle = {
color: "red"
};
var newStyle = {
color: 'green'
};
var group = L.geoJSON(geojson, {
style: startStyle,
pointToLayer: function(feature, latlng) {
point = L.circleMarker(latlng);
assignStyle(point, startStyle);
return point;
}
}).addTo(map);
// Record the style to the individual vector layer if necessary.
function assignStyle(leafletLayer, newStyle) {
leafletLayer.setStyle(newStyle);
leafletLayer._recordedStyle = newStyle;
}
// When desired, apply the style that has been previously recorded.
function reassignStyle(leafletLayer) {
leafletLayer.setStyle(leafletLayer._recordedStyle);
}
document.getElementById('button').addEventListener('click', function(event) {
event.preventDefault();
// Either use the wrapper function that records the new style…
//assignStyle(point, newStyle);
// Or simply modify the group's style option, if it is acceptable affecting all child layers.
point.setStyle(newStyle);
group.options.style = newStyle;
});
group.on({
mouseover: function(e) {
e.target.setStyle({
color: 'blue'
});
},
mouseout: function(e) {
//reassignStyle(e.layer);
group.resetStyle(e.layer);
}
});
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css">
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet-src.js"></script>
<div id="map" style="height: 100px"></div>
<button id="button">Change color to Green…</button>
<p>…then mouseover and mouseout</p>
发布于 2017-12-04 23:25:13
因此,不是
layer.setStyle({ fillOpacity: vm.mapOptions.heatmapOpacity });
使用
L.Util.setOptions(layer, { style: { fillOpacity: vm.mapOptions.heatmapOpacity } });
https://stackoverflow.com/questions/47643487
复制