是否仍然可以指定单层上特征的z顺序

Is there anyway to specify z-order of feature on single layer?

本文关键字:特征 顺序 单层 是否      更新时间:2023-09-26

我使用的是带有三个功能的单层,我想指定z顺序,但无论如何都不能工作。

根据我的理解,功能是按照我添加的相同顺序绘制,所以我尝试按相反的顺序添加,但对我不起作用。

添加功能的代码

var features=[];
jQuery.each(data.route, function (key, val) 
{
  var localfeature = new ol.Feature({ geometry: objGeoJSON.readGeometry(JSON.parse(val.simpleRoute)).transform('EPSG:4326', 'EPSG:3857') });
  localfeature.set("rtype", "R" + (key + 1));
  features.push(localfeature);
});
currentRoute.routingSource.addFeatures(features);

图层的样式功能

//function to apply color based on rtype attribute.
function styleFunction(feature, resolution) {
  var level = feature.get('rtype');
  if (!level || !RouteType[level]) {
    return [defaultStyle];
  }
  if (!styleCache[level]) {
    styleCache[level] = new ol.style.Style({
      stroke: new ol.style.Stroke({
        color: RouteType[level],
        width: 4
      })
    });
  }
  // at this point, the style for the current level is in the cache
  // so return it (as an array!)
  return [styleCache[level]];
}

最后我得到了解决方案,这是将重新排列特征z索引的最终样式函数。

`

    //function to apply color based on rtype attribute.
    function styleFunction(feature, resolution) {
      var level = feature.get('rtype');
      if (!level || !RouteType[level]) {
        return [defaultStyle];
      }
      if (!styleCache[level]) {
        styleCache[level] = new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: RouteType[level],
            width: 4
          }),
          zIndex: zIndexValue
        });
      }
  // at this point, the style for the current level is in the cache
  // so return it (as an array!)
  return [styleCache[level]];
}`

你可以在这里查看示例http://bl.ocks.org/wboykinm/cc509eb2763ca1ba9293