0

与OpenLayers中描述的类似问题:放大或缩小后重新出现被破坏的特征

在矢量图层上调用 destroyFeatures() 或 removeAllFeatures()(或两者,按任意顺序)。特征从视图中消失。但随后放大或缩小,它们会重新出现。对我来说,这只有在使用集群策略时才会发生。似乎集群特征已被破坏,但该集群表示的基础特征并未被破坏,因此,当您放大或缩小时,会根据这些基础特征重新计算聚类并重新绘制。

谷歌搜索揭示了几年前 OpenLayers 开发人员之间关于 destroyFeatures() 问题的一些对话。令人惊讶的是,即使是现在,这些问题似乎还没有完全解决。

我可以通过销毁整个层(使用destroy())然后在需要时重新创建它来解决这个问题。在我的情况下这没问题,但我可以想象这种笨拙的方法可能不受欢迎的情况。

为了响应对代码示例的请求,这里是正在工作的版本中的代码的缩写版本(即,使用destroy())。在非工作版本中,我改为调用destroyFeatures()(并且没有将图层设置为null)。如上所述,这最初会删除特征,但如果我随后使用 this.map.zoomIn() 放大或缩小,这些特征会重新出现。

注意 1:函数是通过 JavaScript 桥从 Objective-C 调用的。注意 2:JavaScript 是使用 CoffeeScript 生成的。

(function() {
  var addSightingsLayer, displayFeaturesForSightingsWithGeoJSONData, geoJSONFormat, load, map, projectionSphericalMercator, projectionWGS84, removeSightingsLayer, sightingsLayer;

  addSightingsLayer = function() {
    var context, layerStyle, layerStyleSelected, style, styleMap, styleSelected, yerClusteringStrategy;
    if (!(this.sightingsLayer === null)) return;
    yerClusteringStrategy = new OpenLayers.Strategy.Cluster({
      distance: 10,
      threshold: 2
    });
    this.sightingsLayer = new OpenLayers.Layer.Vector('Sightings', {
      strategies: [yerClusteringStrategy]
    });
    this.map.addLayer(this.sightingsLayer);
    style = {
      // Here I define a style
    };
    context = {
    // Here I define a context, with several functions depending on whether there is a cluster or not, eg:
      dependentLabel: function(feature) {
        if (feature.cluster) {
          return feature.attributes.count;
        } else {
          return feature.attributes.name;
        }
      }, ....

    };
    layerStyle = new OpenLayers.Style(style, {
      context: context
    });
    styleSelected = {
      //...
    };
    layerStyleSelected = new OpenLayers.Style(styleSelected, {
      context: context
    });
    styleMap = new OpenLayers.StyleMap({
      'default': layerStyle,
      'select': layerStyleSelected
    });
    this.sightingsLayer.styleMap = styleMap;
  };

  removeSightingsLayer = function() {
    if (this.sightingsLayer === null) return;
    this.sightingsLayer.destroy();
    return this.sightingsLayer = null;
  };

  displayFeaturesForSightingsWithGeoJSONData = function(geoJSONData) {
    if (this.sightingsLayer === null) JFOLMap.addSightingsLayer();
    return this.sightingsLayer.addFeatures(this.geoJSONFormat.read(geoJSONData));
  };

  load = function() {
    var lat, lon, osmLayer, zoom;
    lat = ...;
    lon = ...;
    zoom = ...;
    this.map = new OpenLayers.Map('mapDiv', {
      controls: ...,
      eventListeners: ...
    });
    osmLayer = new OpenLayers.Layer.OSM();
    this.map.addLayer(osmLayer);
    return this.map.setCenter(new OpenLayers.LonLat(lon,     lat).transformWGS84ToSphericalMercator(), zoom);
  };

  OpenLayers.LonLat.prototype.transformWGS84ToSphericalMercator = function() {
    return this.transform(JFOLMap.projectionWGS84, JFOLMap.projectionSphericalMercator);
  };

  OpenLayers.LonLat.prototype.transformSphericalMercatorToWGS84 = function() {
    return this.transform(JFOLMap.projectionSphericalMercator, JFOLMap.projectionWGS84);
  };

  map = null;
  sightingsLayer = null;
  sightingsPopoverControl = null;
  projectionWGS84 = new OpenLayers.Projection('EPSG:4326');
  projectionSphericalMercator = new OpenLayers.Projection('EPSG:900913');
  geoJSONFormat = new OpenLayers.Format.GeoJSON({
    'externalProjection': projectionWGS84,
    'internalProjection': projectionSphericalMercator
  });

  this.JFOLMap = {
    map: map,
    sightingsLayer: sightingsLayer,
    projectionSphericalMercator: projectionSphericalMercator,
    projectionWGS84: projectionWGS84,
    geoJSONFormat: geoJSONFormat,
    load: load,
    addSightingsLayer: addSightingsLayer,
    removeSightingsLayer: removeSightingsLayer,
    displayFeaturesForSightingsWithGeoJSONData: displayFeaturesForSightingsWithGeoJSONData,
  };

}).call(this);
4

1 回答 1

0

试试这个,它对我有用

layer.removeAllFeatures();
layer.destroyFeatures();//optional
layer.addFeatures([]);
于 2013-07-23T10:16:05.260 回答