leaflet如何为geoJson图层设置zIndex图层顺序?

这个问题也是纠结了半天,两个geojson的面图层作为Layer,因所画的内容重要程度不同,总有主次之分,比如标记型的图标,即使被盖住了,也无伤大雅,但是一些监控或者告警的数据,如果被盖住了,就比较致命了。

许多建议说使用 Layer.setZIndex(999) ,但是跟过代码知道, Layer.zIndex打印显示undefined,直接官方文档,你会发现GeoJSON.Options 属性中根本没有 zindex属性。

OptionTypeDefaultDescription
pointToLayerFunction*Function defining how GeoJSON points spawn Leaflet layers. It is internally called when data is added, passing the GeoJSON point feature and its LatLng. The default is to spawn a default Marker:
function(geoJsonPoint, latlng) {
    return L.marker(latlng);
}
styleFunction*Function defining the Path options for styling GeoJSON lines and polygons, called internally when data is added. The default value is to not override any defaults:
function (geoJsonFeature) {
    return {}
}
onEachFeatureFunction*Function that will be called once for each created Feature, after it has been created and styled. Useful for attaching events and popups to features. The default is to do nothing with the newly created layers:
function (feature, layer) {}
filterFunction*Function that will be used to decide whether to include a feature or not. The default is to include all features:
function (geoJsonFeature) {
    return true;
}

Note: dynamically changing the filter option will have effect only on newly added data. It will not re-evaluate already included features.

coordsToLatLngFunction*Function that will be used for converting GeoJSON coordinates to LatLngs. The default is the coordsToLatLng static method.
markersInheritOptionsBooleanfalseWhether default Markers for "Point" type Features inherit from group options.

不过可喜的是,你发现了有style属性,跟进去,style中也可以设置 zindex,然在此处的样式加载中并没用处,所以你会很悲哀的发现,你无法通过 data_layer 对象整体设置该 Layer的Zindex。导致界面图标层级不明确。

        this.data_layer = L.geoJSON(data,{
          pointToLayer: PointToLayer,
          onEachFeature: OnEachFeature
        }).addTo(this.map);

        

然后只好百度,然后部分帖子说,使用LayerGroupsetZIndex,查API如下,添加不同的TileLayer确实可以控制层级,然 GeoJSON本身就相当于一个Group,LayerGroup添加

GeoJSON对象时,setZIndex又失去作用了。

那只能往更小的力度去解决了,然后来到了Marker

OptionTypeDefaultDescription
iconIcon*Icon instance to use for rendering the marker. See Icon documentation for details on how to customize the marker icon. If not specified, a common instance of L.Icon.Default is used.
keyboardBooleantrueWhether the marker can be tabbed to with a keyboard and clicked by pressing enter.
titleString''Text for the browser tooltip that appear on marker hover (no tooltip by default).
altString''Text for the alt attribute of the icon image (useful for accessibility).
zIndexOffsetNumber0By default, marker images zIndex is set automatically based on its latitude. Use this option if you want to put the marker on top of all others (or below), specifying a high value like 1000 (or high negative value, respectively).
opacityNumber1.0The opacity of the marker.
riseOnHoverBooleanfalseIf true, the marker will get on top of others when you hover the mouse over it.
riseOffsetNumber250The z-index offset used for the riseOnHover feature.
paneString'markerPane'Map pane where the markers icon will be added. Map pane where the markers shadow will be added.
bubblingMouseEventsBooleanfalseWhen true, a mouse event on this marker will trigger the same event on the map (unless L.DomEvent.stopPropagation is used).

发现有一个zIndexOffset居然好使,代码如下:

    PointToLayer(feature, latlng) {
      var  bili = this.izoom * 3 * (this.izoom / 5.0);
      //落雷  距离线路 小于1000米 使用另一个图标
      return L.marker(latlng, {
          icon: L.icon({
              iconUrl: require("@/assets/image/rep.png"),
              iconSize: [bili,bili],
              iconAnchor: [bili/2,bili/2],
              className: 'map-rep-point'
            }),
           zIndexOffset: 9999 
      });
    },

当然,怀着好奇的心情,试了下 L.icon 元素中的 className 属性中的z-index属性,发现依然控制不住,看来只能在 L.marker中将就使用了。下图放上未调整顺序和未调整的对比:

      

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟●禅●酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值