-
-
Notifications
You must be signed in to change notification settings - Fork 780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add getBounds() method to geoJSON sources #5394
Comments
I think this can be made to be an example, not sure it's worth the maintenance overhead. But I might be too strict... Your call... |
IMHO, it's quite a trivial block of code that
From my point of view, the benefits of adding this method are way above the costs 😄 NOTE: I've updated the snippet I made yesterday in a 5-minute break so that it can avoid extra, unneeded, steps |
Faster (and ES6+) version async function _getbounds() {
const bounds = new maplibregl.LngLatBounds();
const data = await this.getData();
const coordinates = data.features
.map(f => f.geometry.coordinates.flat(Infinity))
.reduce((acc,cur) => [...acc, ...cur],[]);
for(let i = 0; i < coordinates.length - 1; i += 2){
bounds.extend([coordinates[i], coordinates[i+1]]);
}
return bounds;
}
maplibregl.GeoJSONSource.prototype.getBounds = _getbounds; |
As I said, feel free to open a PR, I don't have strong feelings either way... |
Done: #5403 🤓 |
As requested in the Slack thread linked below
https://osmus.slack.com/archives/C01G3D28DAB/p1737627086709479
The code would be as follows
It works regardless the geometry type, or even with mixed geometry types. Testing it with the Add multiple geometries from one GeoJSON source
EDITED: simpler for loop, more performant
The text was updated successfully, but these errors were encountered: