2010-11-30 39 views
1

我有一個OpenLayers.Feature.Vector創建如下:繪圖的MultiPolygon具有不同風格在的OpenLayers每個組件

var multiPol = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.MultiPolygon([polygonGeometry1,polygonGeometry2])); 

兩個多邊形在地圖上表示相同的邏輯對象(一個「實體」),說雲。這就是爲什麼我把它們放在一個功能中。 我想提請它使這個多面(polygonGeometry1,polygonGeometry2)的每個組件繪製時,它的加入層不同的顏色:

var layer = new OpenLayers.Layer.Vector("polygonLayer"); 
    layer.addFeatures([multiPol]); 

我已經採取一看款式,風格的地圖和OpenLayers的規則,但他們似乎不夠。它們使我能夠使用不同的顏色繪製每個幾何類型,但前提是它們屬於不同的特徵(矢量)。有沒有辦法解決這個問題?我真的必須爲每個多邊形使用單獨的Vector嗎?

回答

1

據我所知,要獲得此功能,您需要使用自己的擴展類。

首先爲OpenLayers.Feature.Vector創建一個擴展,將其命名爲YourApp.Feature.MultiVector。您可以通過查看OpenLayers代碼來查看有關如何擴展類的示例。這個類應該接受一組樣式和multiPolygon。它應該有一個方法,將返回一個OpenLayers.Feature.Vectors列表,每個都有自己的樣式。

第二次創建OpenLayers.Layer.Vector的擴展,將其命名爲YourApp.Layer.VectorSupportingMultiStyledFeatures。您將需要重寫「drawFeature」方法。在drawFeature方法測試中查看特徵的類型是否爲MultiVector。如果是,則遍歷MultiVector中的每個要素並調用renderer.drawFeature(feature)。否則,請調用super.drawFeature方法。

所以你的代碼來調用它是這樣的:

var multiPol = new YourApp.Feature.MultiVector(
    new OpenLayers.Geometry.MultiPolygon([polygonGeometry1,polygonGeometry2]), 
    [style1,style2,style3,style4]); 

var layer = new YourApp.Layer.VectorSupportingMultiStyledFeatures("polygonLayer"); 
layer.addFeatures([multiPol]); 
+0

我正要做這樣的事情,但只是想確認有沒有更簡單,更耗時的工作方案。謝謝! – gregorej 2010-12-01 07:57:36

2

提出的解決方案由喬恩·斯奈德與一般的想法提供給我們,但並沒有進行到底(特別是延伸OpenLayers.Layer.Vector工作充分WASN」這是基於OpenLayers 2.1x的任務所必需的)。

我們創建了一個類YourApp.Handler.EndPointsPath(擴展OpenLayers.Handler.Path),其中函數geometryClone()返回類型爲YourApp.Geometry.EndPointsPath的新幾何。

然後我們在OpenLayers.Renderer.Elements修補的功能drawGeometry()得出這個新的幾何形狀:

OpenLayers.Util.extend(OpenLayers.Renderer.Elements.prototype, { 

    drawGeometry: function (geometry, style, featureId) { 
    var cl = geometry.CLASS_NAME; 

    var rendered = true, 
     i, len; 
    if ((cl === "OpenLayers.Geometry.Collection") || 
     (cl === "OpenLayers.Geometry.MultiPoint") || 
     (cl === "OpenLayers.Geometry.MultiLineString") || 
     (cl === "OpenLayers.Geometry.MultiPolygon") || 
     (cl === "YourApp.Geometry.EndPointsPath")) { 
     // Iterate over all Geometry components and draw each individually 
     for (i = 0, len = geometry.components.length; i < len; i++) { 
     // Is there a style for each of the components? 
     if (OpenLayers.Util.isArray(style)) { 
      // Draw Geometry with own style 
      rendered = this.drawGeometry(geometry.components[i], style[i], featureId) && rendered; 
     } else { 
      // Draw Geometry with common style 
      rendered = this.drawGeometry(geometry.components[i], style, featureId) && rendered; 
     } 
     } 
     return rendered; 
    } 
    // (...standard code...) 
    }, 

    eraseGeometry: function (geometry, featureId) { 
    var cl = geometry.CLASS_NAME, 
     i, len; 
    if ((cl === "OpenLayers.Geometry.MultiPoint") || 
     (cl === "OpenLayers.Geometry.MultiLineString") || 
     (cl === "OpenLayers.Geometry.MultiPolygon") || 
     (cl === "YourApp.Geometry.EndPointsPath") || 
     (cl === "OpenLayers.Geometry.Collection")) { 
     for (i = 0, len = geometry.components.length; i < len; i++) { 
     this.eraseGeometry(geometry.components[i], featureId); 
     } 
     // (...standard code...) 
    } 
    } 
}); 
相關問題