【问题标题】:Openlayers 3: Get "undefined" for geojson property in one layerOpenlayers 3:在一层中为 geojson 属性获取“未定义”
【发布时间】:2016-03-26 06:11:59
【问题描述】:

我尝试从 geojson 文件中获取属性 "SCHULNAME"

这是geojson文件中的一个sn-p:

{"type":"FeatureCollection",

"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}},

"features":[{"type":"Feature","properties":{"spatial_name":"01A01",
    "spatial_alias":"1. Schulpraktisches Seminar (S) Mitte",
    "spatial_type":"Point",
    "SCHULNAME":"1. Schulpraktisches Seminar (S) Mitte",
    "SCHULART":"Schulpraktisches Seminar",
    "TRAEGER":"Bezirk",
    "ZWEIG_01":"Schulpraktisches Seminar",
    "ZWEIG_02":null,
    "ZWEIG_03":null,
    "ZWEIG_04":null,
    "BEZIRK":"Mitte",
    "ORTSTEIL":"Wedding",
    "PLZ":"13353",
    "ADRESSE":"Tegeler Str. 16",
    "TELEFON":"4677779713",
    "FAX":"4677779720",
    "EMAIL":"<a href=\"mailto:1.sps-mitte@gmx.de\">1.sps-mitte@gmx.de</a>",
    "INTERNET":null,"LEITUNG":null},
"geometry":{"type":"Point",
    "coordinates":[13.35812948892163,52.54071751171907]}},...

以下是使用属性打开弹出窗口的功能:

map.on('singleclick', function(evt) {
    var feature = map.forEachFeatureAtPixel(evt.pixel,
              function(feature, layer) {
                return feature;
              });
    if (feature) {
    var coordinate = evt.coordinate;
    var printCoord = ol.proj.transform(feature.getGeometry().getCoordinates(), 'EPSG:3857','EPSG:4326');
    var printProps = feature.getProperties();
    PopupContent.innerHTML =    '<table style="width:100%">'+
                                '<tr>' + '<td>'+'<b>Coordinates:</b> ' + Math.round( printCoord[1] * 100 ) / 100 + 'N, ' + Math.round( printCoord[0] * 100 ) / 100 +'E' +'</td>'+'</tr>'+
                                '<tr>'+'<td>'+"<b>Name:</b> " +printProps.SCHULNAME+'</td>'+'</tr>'+
                                '</table>';
    Popup.setPosition(coordinate);


    }});

坐标显示正确。对于属性"SCHULNAME",我得到了结果undefined。但是,如果我对以下 geojson 文件及其属性 "name" 使用相同的代码,它将完美运行:

{"type":"FeatureCollection",
"features":[{"type":"Feature","properties":{"name":"Mitte",
    "description":"",
    "cartodb_id":1,
    "created_at":"2013-09-03T12:32:04+0200",
    "updated_at":"2013-09-03T12:32:04+0200"},
"geometry":{"type":"MultiPolygon",
    "coordinates":[[[[13.403528,52.540212], ...

我在这里错过了什么?

【问题讨论】:

  • 您确定获得了预期的功能吗?
  • 如前所述:坐标显示正确。所以我很确定会得到预期的功能。
  • @user3725485 位坐标只是点击事件的坐标。是否还有其他一些足够接近匹配的功能?
  • @Alvin Lindstam 不。我的意思是创建的 html 文件中的坐标,而不是弹出窗口的位置。你的意思是'var坐标',但是我用'var printCoord'得到的特征坐标
  • 啊,是的。您如何将 GeoJSON 转换为 ol 特征?该功能的属性中是否有任何值?

标签: properties openlayers-3 geojson


【解决方案1】:

我发现了问题的原因,但我不明白为什么会发生这种情况,我希望你能给出解释。

我为第一个 geojson 层创建了一个 ol.source.Cluster 并将该 ol.layer.Vector 层的源引用到该集群源:

var schoolCluster = new ol.source.Cluster({
    source:new ol.source.Vector({
        url: 'Schools.geojson',
        format: new ol.format.GeoJSON(),
    })
});

var Schools = new ol.layer.Vector({
    source: schoolCluster,
    style: iconStyleSchools
  });

但不知何故,只有当我直接在矢量图层中引用源而没有像这样的任何集群时,它才能获取属性:

var Schools = new ol.layer.Vector({
    source:new ol.source.Vector({
        url: 'Schools.geojson',
        format: new ol.format.GeoJSON(),
    }),
    style: iconStyleSchools
  });

使用此代码,我可以从 geojson 文件中获取我想要的所有属性,并且不再获得任何 undefined。有人能解释一下原因吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多