簡體   English   中英

使用json在openlayers.vector上的條件

[英]condition on openlayers.vector using json

我想使用openlayers.vector從json檢索一些信息,但條件是:例如(如果“ type” =“ etat5”)檢索url和名稱

    var geojson_etat7 = new OpenLayers.Layer.Vector("etat7", {
        styleMap: new OpenLayers.StyleMap({
                    "default": new OpenLayers.Style({
                    externalGraphic: './images/Etat${url}.png', 
                    graphicWidth: 21, 
                    graphicHeight: 25,
                    graphicYOffset: -24,
    ===>>>>>>               label : "name: ${name}"

                    } ),

                }),
        projection: epsg4326,
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "data/data_etat5.geojson",
            format: new OpenLayers.Format.GeoJSON()
            })

        });

這是我的數據庫json:

{
"type": "FeatureCollection",
"features": [
{
  "type": "etat5",
  "properties": {
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"5"
    },
  "geometry": {
    "type": "Point",
    "coordinates": [
      26.9140625,
      26.9449741808516
    ]
  }
},
{
  "type": "etat5",
  "properties": {
          "name": "tt",
          "url":"5"
  },
  "geometry": {
    "type": "Point",
    "coordinates": [
     53.15008544921875,
      21.3425828520359735
    ]
  }
},
{
  "type": "etat1",
  "properties": {
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"1"

    },
  "geometry": {
    "type": "Point",
    "coordinates": [
      38.9140625,
      56.9449741808516
    ]
  }
},
   {
  "type": "etat1",
  "properties": {"url":"1"},
  "geometry": {
    "type": "Point",
    "coordinates": [
      -2.15008544921875,
      1.3425828520359735
    ]
  }
},
{
  "type": "etat2",
  "properties": {
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"2"

    },
  "geometry": {
    "type": "Point",
    "coordinates": [
      26.9140625,
      46.9449741808516
    ]
  }
},
   {
  "type": "etat2",
  "properties": {"url":"2"},
  "geometry": {
    "type": "Point",
    "coordinates": [
     3.15008544921875,
      11.3425828520359735
    ]
  }
},
{
  "type": "etat3",
  "properties": {
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"3"
    },
  "geometry": {
    "type": "Point",
    "coordinates": [
      16.9140625,
      26.9449741808516
    ]
  }
},
   {
  "type": "etat3",
  "properties": {"url":"3"},
  "geometry": {
    "type": "Point",
    "coordinates": [
     23.15008544921875,
      31.3425828520359735
    ]
  }
},
  {
  "type": "etat4",
  "properties": {
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"4"
    },
  "geometry": {
    "type": "Point",
    "coordinates": [
      3.9140625,
      16.9449741808516
    ]
  }
},
   {
  "type": "etat4",
  "properties": {"url":"4"},
  "geometry": {
    "type": "Point",
    "coordinates": [
     23.15008544921875,
      71.3425828520359735
    ]
  }
},
{
  "type": "etat6",
  "properties": {
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"6"
    },
  "geometry": {
    "type": "Point",
    "coordinates": [
      16.9140625,
      36.9449741808516
    ]
  }
},
   {
  "type": "etat6",
  "properties": {"url":"6"},
  "geometry": {
    "type": "Point",
    "coordinates": [
     43.15008544921875,
      111.3425828520359735
    ]
    }
   }
 ]
}

您無法執行此操作,因為GeoJSON解析器使用type來確定Geometry,Feature和FeatureCollection之間的要素類型,請參閱read方法的注釋。

但是,將對屬性進行解析,並且這些屬性成為從GeoJSON.read函數返回的功能向量的屬性的一部分。 如果將類型放入屬性中(假設您可以更改結構),那么它將被拾取並添加為特征的屬性,然后可以像這樣訪問該屬性:

var reader=new OpenLayers.Format.GeoJSON();
var features=reader.read(json);
for (var i=0; i<features.length; i++){
    if(features[i].attributes['type']==='etat5'){
         console.log(features[i].attributes['url']);
    }
};

以及JSON如何與此一起工作的示例。

var json={
  "type": "FeatureCollection",
  "features": [{  
     "properties": {
        "type": "etat5",
        "name": "test",
        "amenity": "toto",
        "popupContent": "popo",
        "url":"5"
    },
     "geometry": {
       "type": "Point",
       "coordinates": [
       26.9140625,
       26.9449741808516
     ]
    } 
  }
]};

var geojson_etat7 = new OpenLayers.Layer.Vector(“ etat7”,{styleMap:new OpenLayers.StyleMap({

                    "default": new OpenLayers.Style({
                    externalGraphic: './images/Etat${nom}.png', 
                    graphicWidth: 21, 
                    graphicHeight: 25,
                    graphicYOffset: -24,
                    label : "${tot}"
                    },
                    { context: {
                        tot: function(feature){
                            var tt=" etat7";
                            //return tt;
                            if(feature.attributes.url=="4"){
                            return  tt;
                            }


                        },
                        nom: function(feature){
                            var tt="4";
                            //return tt;
                            if(feature.attributes.url=="4"){
                            return  tt;
                            }


                        }
                    }
                    } )  

                }),
        projection: epsg4326,
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.HTTP({
            url: "data/data_etat5.geojson",
            format: new OpenLayers.Format.GeoJSON()
            })

        });

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM