Is there anyway to specify z-order of feature on s

2019-04-11 00:01发布

I am using single layer with three feature and would like to specify z-order but not working anyway.

As per my understanding feature is drawing in same order in which I am adding so I tried by adding in reverse order but it not worked for me.

Code to add feature

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);

Style function of layer

//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]];
}

1条回答
Juvenile、少年°
2楼-- · 2019-04-11 00:28

Finally I got solution,Here is the final style function that will rearrange feature z-index.

`

    //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]];
}`

you can check example here http://bl.ocks.org/wboykinm/cc509eb2763ca1ba9293

查看更多
登录 后发表回答