You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there no way to set the initial bounds of a Map?
options.maxBounds allows one to set the initial bounding box, but also permanently constrains the view to this box.
A combination of .center and .zoom would effectively allow one to set initial bounds, but also requires some computation, and is inexact.
Is there an easy way to set an initial options.bounds ?
Reacted by Andrew Harvey, nate-at-niche, Luke F, Michael Keyser, Igal, Andrey Pissantchev, Cameron Little, Ian Trewella, Zsolt Ero, Magnus Hiie and 23 more
To get around this at the moment, I've used the 'geojsonExtent' mapbox.js library to facilitate zooming to bounds. Agreed it would be great to have as a standard feature in the API.
<scriptsrc='https://api.mapbox.com/mapbox.js/plugins/geojson-extent/v0.0.1/geojson-extent.js'></script>functionfit(){//fit gl map to a geojson file boundstry{map.fitBounds(geojsonExtent(geojson_data));}catch(err){console.log(err);}}
@ryanbaumann I think there's a misunderstand there. I believe what @peckjon is asking for is not a method to to zoom to a bounding box (Map#fitBounds) per se, because this is actually already part of the library. The problem is that the current function is animated so you can't instantly change the center/zoom of a map like you can with Map#jumpTo, using a bounding box. What this ticket asks for is a fitBounds without a transition (and possibly also as input when instantiating the map).
@jfirebaugh Gotcha, but I still got the impression that @ryanbaumann was asking for functionality in the API to actually calculate the bounds for you based on a given geometry, i.e. something like "Map#fitGeojson(geojson)"
My intent was to communicate that from a user perspective, I want to initialize a map to fit the bounds of a data layer. Zooming to bounds after the data layer is added to the map is acceptable.
This is missing for my project too!
I am calculating the bounds from an existing list of coordinates, e.g., like this:
vardevicesBounds=newmapboxgl.LngLatBounds();// iterate over all markersfor(vari=0;i<devices.length;i++){vardevice=devices[i];// add the current coordinates to the bounds array, so we can fit the map extent to the bounds of all markersdevicesBounds.extend([device.longitude,device.latitude]);}
And then I am setting the map config parameters like this
Being able to specify an initial bounds when the map is instantiated would be very convenient - we definitely have a need for that. Currently I will work around this by calculating the center pt and zoom based on the bounding box and the div dimensions, but even better to have this built in to the api. Bonus points for integrating with the camera options and setting the center & zoom from the bounds, and the bearing and pitch from the camera options.
Activity
ravraponi commentedon Jan 22, 2016
+1
On Fri, Jan 22, 2016 at 2:12 AM, Jon Peck [email protected] wrote:
lucaswoj commentedon Jan 22, 2016
You are correct. That feature does not exist but should 👍
[-]No way to set initial Bounds?[/-][+]Add a way to set initial map bounds[/+]ryanbaumann commentedon Jan 24, 2016
To get around this at the moment, I've used the 'geojsonExtent' mapbox.js library to facilitate zooming to bounds. Agreed it would be great to have as a standard feature in the API.
averas commentedon Jan 26, 2016
@ryanbaumann I think there's a misunderstand there. I believe what @peckjon is asking for is not a method to to zoom to a bounding box (Map#fitBounds) per se, because this is actually already part of the library. The problem is that the current function is animated so you can't instantly change the center/zoom of a map like you can with Map#jumpTo, using a bounding box. What this ticket asks for is a fitBounds without a transition (and possibly also as input when instantiating the map).
Also, getting the bounding box for a geometry I would argue is out of scope for the library, but can easily done with for example turf.js (http://turfjs.org/static/docs/module-turf_extent.html).
jfirebaugh commentedon Jan 26, 2016
Unanimated
Map#fitBoundsis #1473. This issue is about bounds-based map initialization.averas commentedon Jan 26, 2016
@jfirebaugh Gotcha, but I still got the impression that @ryanbaumann was asking for functionality in the API to actually calculate the bounds for you based on a given geometry, i.e. something like "
Map#fitGeojson(geojson)"ryanbaumann commentedon Jan 27, 2016
@averas I was off-topic on my comment. Apologies.
My intent was to communicate that from a user perspective, I want to initialize a map to fit the bounds of a data layer. Zooming to bounds after the data layer is added to the map is acceptable.
anx-ckreuzberger commentedon Feb 10, 2017
This is missing for my project too!
I am calculating the bounds from an existing list of coordinates, e.g., like this:
And then I am setting the map config parameters like this
However, this is far from optimal, as the zoom level is static. I would like to do it like this:
lucaswoj commentedon Feb 13, 2017
Closely related to #4029
jwangler commentedon Apr 26, 2017
+1
Being able to specify an initial bounds when the map is instantiated would be very convenient - we definitely have a need for that. Currently I will work around this by calculating the center pt and zoom based on the bounding box and the div dimensions, but even better to have this built in to the api. Bonus points for integrating with the camera options and setting the center & zoom from the bounds, and the bearing and pitch from the camera options.
21 remaining items
fix map initial bounds test mapbox#1970
resize map before setting initial view mapbox#1970
add initial bounds as map constructor option mapbox#1970
fix map initial bounds test mapbox#1970
resize map before setting initial view mapbox#1970
peckjon commentedon Aug 20, 2018
See #1473 (comment)
add initial bounds as map constructor option mapbox#1970
fix map initial bounds test mapbox#1970
resize map before setting initial view mapbox#1970
move this.resize() back to conditional mapbox#1970
fix expected zoom value for initial bounds test mapbox#1970
add initial bounds as map constructor option #1970 (#5518)
korywka commentedon Oct 18, 2018
duration 0 is not a good solution:
mourner commentedon Oct 18, 2018
This was fixed in #5518