Skip to content

Add a way to set initial map bounds #1970

Closed

Description

@peckjon

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 ?

Activity

  1. ravraponi commented on Jan 22, 2016

    @ravraponi

    +1

    On Fri, Jan 22, 2016 at 2:12 AM, Jon Peck [email protected] wrote:

    Is there no way to set the initial bounds of a Map?

    optionsmaxBounds https://wwwmapboxcom/mapbox-gl-js/api/#Map 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 optionsbounds ?

    —
    Reply to this email directly or view it on GitHub
    #1970.

  2. lucaswoj commented on Jan 22, 2016

    @lucaswoj
    Contributor

    You are correct. That feature does not exist but should 👍

  3. changed the title [-]No way to set initial Bounds?[/-] [+]Add a way to set initial map bounds[/+] on Jan 22, 2016
  4. ryanbaumann commented on Jan 24, 2016

    @ryanbaumann
    Contributor

    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.

    <script src='https://api.mapbox.com/mapbox.js/plugins/geojson-extent/v0.0.1/geojson-extent.js'></script>
    function fit() {
        //fit gl map to a geojson file bounds
        try {
            map.fitBounds(geojsonExtent( geojson_data ));
        }
        catch (err) {
            console.log(err);
        }
    }
  5. averas commented on Jan 26, 2016

    @averas
    Contributor

    @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).

  6. jfirebaugh commented on Jan 26, 2016

    @jfirebaugh
    Contributor

    Unanimated Map#fitBounds is #1473. This issue is about bounds-based map initialization.

  7. averas commented on Jan 26, 2016

    @averas
    Contributor

    @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)"

  8. ryanbaumann commented on Jan 27, 2016

    @ryanbaumann
    Contributor

    @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.

  9. anx-ckreuzberger commented on Feb 10, 2017

    @anx-ckreuzberger

    This is missing for my project too!
    I am calculating the bounds from an existing list of coordinates, e.g., like this:

    var devicesBounds = new mapboxgl.LngLatBounds();
    
    // iterate over all markers
    for (var i = 0; i < devices.length; i++)
    {
        var device = devices[i];
    
        // add the current coordinates to the bounds array, so we can fit the map extent to the bounds of all markers
        devicesBounds.extend([
            device.longitude,
            device.latitude
        ]);
    }

    And then I am setting the map config parameters like this

    mapboxConfig.center = devicesBounds.getCenter();
    mapboxConfig.zoom = 14;

    However, this is far from optimal, as the zoom level is static. I would like to do it like this:

    mapboxConfig.initialBounds = devicesBounds;
  10. lucaswoj commented on Feb 13, 2017

    @lucaswoj
    Contributor

    Closely related to #4029

  11. jwangler commented on Apr 26, 2017

    @jwangler

    +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.

  12. 21 remaining items

  13. peckjon commented on Aug 20, 2018

    @peckjon
    ContributorAuthor
  14. korywka commented on Oct 18, 2018

    @korywka
    Contributor

    duration 0 is not a good solution:

    1. you still see jumping map after load
    2. you pay for extra tiles 💸 💸 💸
  15. mourner commented on Oct 18, 2018

    @mourner
    Member

    This was fixed in #5518

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions