question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Layer.getExtent() returns undefined when working with a Layer sourced by a GPX file

See original GitHub issue

the problem

When trying to work with a Layer sourced by a GPX file, I had a hard time trying to figure out why gpxLayer.getExtent() was undefined… until I found out that gpxLayer.getSource().getExtent() was the right way to get its extent.

the proposed solution

I think it would be easier for newcomers if a method like Layer.getExtent() always return the correct extent, no matter what is behind the scenes, whether it is a Tile layer, a Vector layer or an Overlay layer.

some sample code

// this is the code that works
map.getView().fit(buffer(gpxLayer.getSource().getExtent(), 1000))

// this is the code that doesn't work nowadays
map.getView().fit(buffer(gpxLayer.getExtent(), 1000))

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ahocevarcommented, Feb 13, 2020

The layer’s getExtent() function returns the layer extent, if you specified one to restrict the visible area of the layer. This is documented in https://openlayers.org/en/latest/apidoc/module-ol_layer_Vector-VectorLayer.html#getExtent.

This is because in OpenLayers, the Layer is a lightweight container that configures things that influence the way the layer is rendered (e.g. styles, rendered extent, etc.). Whereas the Source represents the underlying data. So to get the extent of the data, you’ll need to ask the source for the extent.

1reaction
bmndscommented, Feb 14, 2020

I see… I will close this issue then.

But maybe this explanation could be added to VectorLayer.getExtent docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the extent of a GPX file to display it on a map ...
The source won't load unless the layer is visible on the map. ... getExtent()); }); // open the view to force a load...
Read more >
layer get extent returns null openlayers 4 - GIS Stack Exchange
In your code, you are using getExtent for layer1 . According to the API docs, it "returns the extent of the layer".
Read more >
OpenLayers v7.2.2 API - Class: Layer
When undefined , a zIndex of 0 is assumed for layers that are added to the map's layers collection, or Infinity when the...
Read more >
OpenLayers 3 API Reference - Class: Vector
Returns : The contrast of the layer. getExtent(){ol.Extent|undefined} inherited.
Read more >
CSV, TXT, and GPX files—ArcGIS Online Help | Documentation
When you add a CSV file without location information to Map Viewer, a table layer is published and added to the map. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found