簡體   English   中英

如何使用 Leaflet 上的 FeatureCollection 屬性中的參數設置多邊形的樣式?

[英]How can I style polygons with parameters inside the FeatureCollection properties on Leaflet?

我有一個 GeoJSON 文件,其中包含FeatureCollectionFeature類型。 基本上我的代碼如下(這里的工作示例):

L.geoJSON(myJSON, { style: style }).addTo(mymap);

function style(featureObject) {
    return {
        fillColor: getColor(featureObject.properties.name),
        weight: 2,
        opacity: 1,
        color: 'white',
        dashArray: '3',
        fillOpacity: 0.7
    };
}

function getColor(name) {
    return name === "name1" ? "#CF8562" :
            name === "name2" ? "#FFEDA0" :
                                "#000000";
}

我的 GeoJSON 結構的一個小樣本如下:

[
  {
    "type": "FeatureCollection",
    "properties": {
        "name" : "name1"
    },
    "features": [
      {
        "type": "Feature",
        "properties": {
            "randomParameter" : "text"
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
            [
            -46.6479847244904,
            -23.553060554172923
            ],
            [
            -46.6479847244904,
            -23.553060554172923
            ],
            [
            -46.64805562776844,
            -23.55318890961171
            ],
            [
            -46.64826795788915,
            -23.552928264599316
            ],
            [
            -46.6479847244904,
            -23.553060554172923
            ]
            ]
          ]
        }
      }]
  },
  {
    "type": "FeatureCollection",
    "properties": {
        "name" : "name2"
    },
    "features": [
      {
        "type": "Feature",
        "properties": {
            "randomParameter" : "text2"
        },
        "geometry": {
          "type": "Polygon",
          "coordinates": [
            [
            [
            -46.648050596629034,
            -23.55393832474017
            ],
            [
            -46.64758222900779,
            -23.554141824100373
            ],
            [
            -46.64767437978837,
            -23.554322319586415
            ],
            [
            -46.64814729501603,
            -23.55411425749883
            ],
            [
            -46.648050596629034,
            -23.55393832474017
            ]
            ]
          ]
        }
      }]
  }
]

此代碼無法正常工作,因為它無法識別featureObject.properties.name值。 此值僅存在於FeatureCollection中,而不存在於我的 GeoJSON 中的Feature類型中。 因此,我無法根據 GeoJSON 中的數據為多邊形着色。

我想編輯FeatureCollection類型下所有元素的樣式。 但是,當我打印 function style的 object 時,我看到它只看到具有Feature類型的元素。 我讀過這樣的答案,告訴我們可以編輯FeatureCollection類型的樣式,但我沒有設法讓它工作。

我想解決這個問題,而不必更改我的 GeoJSON 結構或將FeatureCollection中的屬性更改為Feature 可能嗎? 我可以讓樣式 function 更改FeatureCollection類型下所有內容的樣式,而不是 GeoJSON 中的Feature類型嗎?

您可以首先遍歷所有 FeatureCollections 並將屬性復制到子項:

https://jsfiddle.net/falkedesign/9yvb46o5/

function copyProps(geojson, props){
    var features = L.Util.isArray(geojson) ? geojson : geojson.features,
      i, len, feature;

    if(props && geojson.properties){
      geojson.properties = Object.assign({},props,geojson.properties); // merge properties, if the feature has the same property as the parent, it will not overwritten
    }

    if (features) {
      for (i = 0, len = features.length; i < len; i++) {
        feature = features[i];
        if (feature.geometries || feature.geometry || feature.features || feature.coordinates) {
          copyProps(feature, geojson.properties);
        }
      }
    }
  }
copyProps(myJSON);
var layersObj = L.geoJSON(myJSON, { style: style }).addTo(mymap);

我可以在里面有一個 leaflet divIcon 的 className <style scoped> section of a vue component?</div><div id = 'text_translate'><p>I have a Vue 2 sample project at <a href="https://github.com/ericg-vue-questions/leaflet-test" rel="nofollow noreferrer">https://github.com/ericg-vue-questions/leaflet-test</a></p> <p>When <a href="https://github.com/ericg-vue-questions/leaflet-test/blob/main/src/components/LeafletTest.vue" rel="nofollow noreferrer">declaring the divIcon</a>, I have:</p> <pre> const cloudIcon = L.divIcon({ html: thecloud, className: 'my-custom-icons', iconSize: [size, size], iconAnchor: [size/2, size/2] }) </pre> <p>and, for leaflet to see the my-custom-icons style, I wrote:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } &lt;/style&gt; &lt;style&gt; .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>I would like the style section to look like:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>But when I place my-custom-icons inside of the components scoped section, the divIcon cannot find it.</p> <p>Can I organize my style section like this? If so, how?</p> </div></style>

[英]Can I have the className of a leaflet divIcon inside the <style scoped> section of a vue component?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何在傳單中設置geoJSON FeatureCollection的樣式? 反應 Leaflet 多邊形不更新屬性 如何通過坐標從JSON FeatureCollection查找屬性值? 如何使用自定義創建的 mapbox 樣式和傳單 如何在 leaflet.js 中設置 layer.control 的樣式? 傳單:如何在沒有樣式屬性的GeoJSON對象中設置2000+點的樣式? 如何更改此元素的樣式屬性? 如何在傳單中找到相鄰的多邊形 我可以在里面有一個 leaflet divIcon 的 className <style scoped> section of a vue component?</div><div id = 'text_translate'><p>I have a Vue 2 sample project at <a href="https://github.com/ericg-vue-questions/leaflet-test" rel="nofollow noreferrer">https://github.com/ericg-vue-questions/leaflet-test</a></p> <p>When <a href="https://github.com/ericg-vue-questions/leaflet-test/blob/main/src/components/LeafletTest.vue" rel="nofollow noreferrer">declaring the divIcon</a>, I have:</p> <pre> const cloudIcon = L.divIcon({ html: thecloud, className: 'my-custom-icons', iconSize: [size, size], iconAnchor: [size/2, size/2] }) </pre> <p>and, for leaflet to see the my-custom-icons style, I wrote:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } &lt;/style&gt; &lt;style&gt; .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>I would like the style section to look like:</p> <pre>&lt;style scoped&gt; #mapContainer { width: 500px; height: 500px; } .my-custom-icons { background-color: red; } &lt;/style&gt; </pre> <p>But when I place my-custom-icons inside of the components scoped section, the divIcon cannot find it.</p> <p>Can I organize my style section like this? If so, how?</p> </div></style> Leaflet:如何僅在多邊形內顯示圖層
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM