无论如何在单层上指定特征的z顺序?

时间:2016-01-12 11:38:30

标签: javascript openlayers-3

我正在使用具有三个功能的单层,并且想要指定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]];
}

1 个答案:

答案 0 :(得分:4)

最后我得到了解决方案,这是最终的样式函数,它将重新排列特征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]];
}`

您可以在此处查看示例http://bl.ocks.org/wboykinm/cc509eb2763ca1ba9293