- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 map 上渲染了具有初始不透明度的 geoJson map 区域。我有一个 slider 可以动态更改不透明度。
这是我动态设置不透明度的部分( typescript ),这是我在自定义传单控件中对输入更改事件执行的:
this.layers.forEach((r: L.GeoJSON<any>) => {
r.eachLayer((layer: any) => {
layer.setStyle({ fillOpacity: vm.mapOptions.heatmapOpacity });
});
});
setTimeout(() => this.map.invalidateSize());
我还可以将鼠标悬停在区域上,在这种情况下,我会降低不透明度并在鼠标悬停时在事件区域上放置边框。
当他们离开该区域时,它当前在该区域上使用 resetStyles 将其重置回以前的样式。我在选项 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 将样式设置为具有新不透明度的我的值,而不是创建区域时设置的原始不透明度?我该怎么做?
最佳答案
resetStyle
Leaflet GeoJSON 层组的方法重新应用 style
在创建该组时应用的样式,如果您没有提供样式,则使用默认样式:
Resets the given vector layer's style to the original GeoJSON style, useful for resetting style after hover events.
如果您稍后更改该组中一个或所有矢量图层的样式,则当您使用 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>
关于leaflet - Leaflet中如何让一个样式成为默认样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643487/
我正在尝试实现类似于 Leaflet.buffer 的缓冲区在我的 map 上。然而,Leaflet.buffer 似乎只适用于 Leaflet.Draw。 我正在使用 Leaflet Editabl
我正试图摆脱 Google map ,因为我受够了他们现在的速度有多慢!我决定试一试 Leaflet,这似乎是个不错的选择。下面是我的代码: initMap(); function initMap()
在 Leaflet 中,是否可以使用 {clickable:false} 定义标记或折线,以便点击传递到下方的任何内容 - 无论是 map 还是可点击的几何对象? 目前我通过使标记/折线可点击并自己传
我正在结合 Leaflet 使用 HTML map 进行一些测试。 .服务器端我有一个 Ruby Sinatra 应用程序,它提供由 MySQL 表获取的 json 标记。使用 2k-5k 和可能更多
我无法使用Leaflet(0.7.7)/Leaflet.Draw(latest)将Layer属性转换为GEOJson对象的属性。我的工作流程是: 1创建 map :var map = L.map('#
我正在使用使用传单 api 的应用程序。 简介 我需要绘制不同类型的围栏,使用装饰器我可以在一定程度上为折线应用良好的视觉效果,但不多。 问题 我愿意显示双绞线而不是破折号、点或普通线,我知道双绞线将
我在我的应用程序中使用 WebView 来显示 Leaflet map 。 在 HTML 文件中,我有以下信用和链接: L.tileLayer('https://api.tiles.mapbox.co
我正在使用https://github.com/Leaflet/Leaflet.draw插件,并且试图检索已编辑图层的图层类型。 在draw:created事件中,我有layer和layerType,
我的左下角有缩放控件和比例尺。 我遇到的问题是,当我的 map 在移动设备上旋转时,它们与左上角的自定义控制面板重叠。 那么如何使比例尺水平位于缩放控件旁边? 最佳答案 您可以通过更改比例控件的 CS
我想计算标记的高度,但我在传单中没有找到任何解决方案。我找到了海拔插件,它允许在 map 上绘制海拔剖面图,但我不需要它。你知道如何计算高度吗?谢谢。 最佳答案 编辑:一个不错的选择 为任何可能需要它
我明白我可以使用通用Leaflet layer ,以及更高级的 map 框 featureLayer ,它提供了作为过滤器的有用功能。但是,我不明白两者之间的区别 marker = L.Marker
我正在尝试在 Render mapbox vector tiles inside react-leaflet? 中描述此实现工作,除了我使用的是 TypeScript。 所以我的文件看起来像这样: i
我正在尝试将我的点在 map 上的正确位置作为 Latlng: let point = L.point(0,0) // x=0,y=0 let latlng = map.unproject(point
我正在使用 Leaflet.VectorGrid用于在传单 map 上加载 pbf 矢量切片的插件。我检索矢量切片的 API 需要传递授权 header 。在 Mapbox GL js 中,这可以通过
我们有一张 map ,在某个缩放级别,我们开始对标记进行聚类。 现在我希望能够删除某些标记。我可以删除不参与集群的标记,但集群中的标记不会被删除,因为代码不会遍历它们。 我会发布代码,但它无处不在,而
我仅在同一位置的标记上进行聚类(即 maxClusterRadius = 0)。单击集群时,我希望它以特定的缩放级别(不是最大缩放)居中并放大,然后立即 spiderfy。使用以下代码,spiderf
使用 Leaflet javascript。我希望我的“清除按钮”能做两件事... 1) 取消选中所有 L.Control 层2) 从 map 中移除当前叠加层 我可以使用这段代码轻松完成第一个: v
我正在开发一个包含大量标记的应用程序,并希望将它们聚类。我找到了 Leaflet.markercluster,它做得很好。但是,我想自定义标记的聚类。具体来说,我想根据标记所在的国家/地区对我的标记进
有点奇怪,希望有人能帮忙。 在传单中,一旦用户输入了纬度/经度并向 map 添加了一个点,我希望能够在该点周围添加一个 10 公里的正方形。 我已经尝试四处寻找计算以找到 x 公里外的正方形的角,但没
我在 leaflet.js 中创建了一个具有多个图层的 map ,但无法弄清楚如何切换一个图层以最初显示在 map 上。 Here is the link 我确信它相当简单,但就是无法弄清楚。 最佳答
我是一名优秀的程序员,十分优秀!