简体   繁体   English

在Openlayers中获取GeoJSON数据

[英]Grabbing GeoJSON data in Openlayers

What I'm trying to do: 我正在尝试做的是:

Figure out how to reference/grab geoJSON data from a server. 了解如何从服务器引用/获取geoJSON数据。

In this case I'm just using an example on the openLayers doc. 在这种情况下,我只是在openLayers文档上使用示例。

Ideally I'd just be able to print out a features ID/type, but I cannot get it to work. 理想情况下,我只能打印出功能ID /类型,但无法正常工作。

What's happening: 发生了什么:

  var selectElement = document.getElementById('type');

  var source = vector.getSource();

  var feature = source.getFeatures()[0];

  var changeInteraction = function() {
    if (select !== null) {
      map.removeInteraction(select);
    }
    var value = selectElement.value;
    if (value == 'singleclick') {
      select = selectSingleClick;
    } else if (value == 'click') {
      select = selectClick;
    } else if (value == 'pointermove') {
      select = selectPointerMove;
    } else if (value == 'altclick') {
      select = selectAltClick;
    } else {
      select = null;
    }
    if (select !== null) {
      map.addInteraction(select);
      select.on('select', function(e) {
        document.getElementById('status').innerHTML = feature.getGeometry().getType();
      });
      console.log(feature);
    }
  };

I was hoping my innerHTML would display "Polygon" in this case, but no such luck. 我希望我的innerHTML在这种情况下可以显示“多边形”,但是没有这种运气。 I've tried various combinations, and been looking over the documentation, can't see what I'm doing wrong. 我尝试了各种组合,并查看了文档,看不到我在做什么错。

The server I'm trying to grab the info from is, 我尝试从中获取信息的服务器是

https://openlayers.org/en/v4.6.4/examples/data/geojson/countries.geojson https://openlayers.org/en/v4.6.4/examples/data/geojson/countries.geojson

Any help would be appreciated. 任何帮助,将不胜感激。

(I can attach full code if helpful) (如果有帮助,我可以附加完整的代码)

I was able to replicate your program and find the solution for retrieving the Country's name for a selected feature, as mentioned in your comments. 如您的评论所述,我能够复制您的程序并找到用于检索所选功能的国家/地区名称的解决方案。

First, remove the following lines. 首先,删除以下几行。 You don't want the first feature of the source file but the first selected feature instead. 您不想要源文件的第一个功能,而是想要第一个选定的功能。

var source = vector.getSource();

var feature = source.getFeatures()[0];

Second, define the feature inside the callback function(e) for the select Event. 其次,在select事件的回调函数(e)中定义功能。 Also, since getFeatures() will return a Collection of features the getArray() method is necessary. 另外,由于getFeatures()将返回功能集合,因此必须使用getArray()方法。

The get(key) method will return a value for a determined key, "name" in this case. get(key)方法将为确定的键(在这种情况下为“ name”)返回一个值。

if (select !== null) {
  map.addInteraction(select);
  select.on('select', function(e) {

    var feature = e.target.getFeatures().getArray()[0];

    document.getElementById('status').innerHTML = ' ' +

    feature.get("name") + '  ' + feature.getId();

});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM