0

I'm trying to parse and display a geojson representation of the UK postcode districts on a Here Map using the JavaScript API within a VueJs application.

The code is relatively simple - districtGeojson is the JSON document. The following function is called after the map has been initialised and displayed:

processGeojson() {
  const reader = new H.data.geojson.Reader(districtGeojson, {
    disableLegacyMode: true
  });

  reader.parse();
  const layer = reader.getLayer();

  this.shapes = layer;
  console.log(layer.isValid(7));
  try {
    this.map.addLayer(layer);
  } catch (err) {
    console.log('err adding layer', err);
  }
}

As you can see, there's a console.log() in there to do some kind of checking on the layer's validity at the default zoom level and it returns true.

All I see is the map flicker briefly and then the plain map is shown. Is there a way to get some feedback from the API on what is going wrong, it seems to just fail silently - addLayer throws no exception?

If necessary, I can share the JSON document but as it's large (5Mb) I wanted to see if there was anything obviously wrong with this code first.

Ross Coundon
  • 647
  • 1
  • 9
  • 20

1 Answers1

0

So, it appears there may be an error in the documentation here: https://developer.here.com/documentation/maps/3.1.17.0/api_reference/H.data.geojson.Reader.html#getLayer

which states:

var reader = new H.data.geojson.Reader('/path/to/geojson/file.json');
reader.parse();
// Assumption: map already exists
map.addLayer(reader.getLayer());

Whereas what just worked for me was to call parseData() rather than parse(), passing the source data. I can do:

var reader = new H.data.geojson.Reader('');
reader.parseData(districtGeojson);
// Assumption: map already exists
map.addLayer(reader.getLayer());

But not

var reader = new H.data.geojson.Reader('./Districts.json');
reader.parse();
// Assumption: map already exists
map.addLayer(reader.getLayer());

Which begs the question of why the file path is required in the constructor as I cannot get the reader to parse data that is passed as a path in the constructor.

Ross Coundon
  • 647
  • 1
  • 9
  • 20