0

我正在尝试在 Here Maps 中使用 GeoJson。我是 Here Maps API 的新手,所以我按照官方示例加载 GeoJson,如果我从 url 加载 GeoJson,一切正常。现在我想从 JS 对象加载 GeoJson,但该geojson.Reader方法似乎只允许读取 url。是否可以加载对象?

var myGeoJsonObject = ...

function showGeoJSONData (map) {

var reader = new H.data.geojson.Reader(myGeoJsonObject), {

    style: function (mapObject) {

      if (mapObject instanceof H.map.Polygon) {
        mapObject.setStyle({
          fillColor: 'rgba(255, 0, 0, 0.5)',
          strokeColor: 'rgba(0, 0, 255, 0.2)',
          lineWidth: 3
        });
      }
    }
  });

  reader.parse();

  map.addLayer(reader.getLayer());
}
4

2 回答 2

0

在这种情况下需要使用 parseData,H.data.geojson.Reader.parseData:https ://developer.here.com/documentation/maps/3.1.22.0/api_reference/H.data.geojson.Reader.html#解析数据

    const reader = new H.data.geojson.Reader(null, {
    style: function (mapObject) {
      if (mapObject instanceof H.map.Polygon) {
        mapObject.setStyle({
          fillColor: 'rgba(255, 0, 0, 0.5)',
          strokeColor: 'rgba(0, 0, 255, 0.2)',
          lineWidth: 3
        });
      }
    }
 });
// pass the data here
reader.parseData(myGeoJsonObject);
于 2021-02-12T12:46:14.540 回答
0

docs看来,您有两个选择:

  1. 将文件路径传递给构造函数:
const reader = new H.data.geojson.Reader('/path/to/geojson/file.json');
reader.parse();
  1. 或者,您可以创建一个新实例并且不向构造函数传递任何内容。然后,您可以在实例上调用该.parse方法并传递您的 GeoJSON 对象。您仍然可以将选项对象传递给构造函数:
const reader = new H.data.geojson.Reader(null, {
    style: function (mapObject) {
      if (mapObject instanceof H.map.Polygon) {
        mapObject.setStyle({
          fillColor: 'rgba(255, 0, 0, 0.5)',
          strokeColor: 'rgba(0, 0, 255, 0.2)',
          lineWidth: 3
        });
      }
    }
  });

// pass the data here
reader.parse(myGeoJsonObject);
于 2020-11-25T20:06:02.093 回答