Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapbox-gl height 100%

Mapbox doesn't fit to it's container. whay not?

This is the rendered html:

<div class="map mapboxgl-map" id="mapBox">
  <div class="mapboxgl-canvas-container">
    <canvas class="mapboxgl-canvas" style="position: absolute; width: 1920px; height: 277px;" tabindex="0" aria-label="Map" width="1920" height="277">
    </canvas>
  </div>
</div>

those 277px are the default I guess.

this is the js:

mapboxgl.accessToken = 'blabla';
  var map = new mapboxgl.Map({
    container: 'mapBox',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: [-77.04, 38.907],
    zoom: 11.15
  });

this is the scss:

.map {
  grid-column: 1/-1;
  height: 100%;
  position: relative;
  canvas, .mapboxgl-canvas {
    height: 100%;
  }
}

If I add the ever so famous !important to the height: 100%; then it works but the map is stretched.

How do I have to do this?

like image 378
KSPR Avatar asked Jul 23 '19 14:07

KSPR


People also ask

What is BBOX in Mapbox?

A bounding box is a mechanism for describing a particular area of a map.

How does Mapbox GL work?

Mapbox GL fetches geospatial data from vector tiles and style instructions from a style document and helps the client draw 2D and 3D Mapbox maps as dynamic visual graphics with OpenGL.

What are layers in Mapbox?

The type of layer is specified by the "type" property, and must be one of background , fill , line , symbol , raster , circle , fill-extrusion , heatmap , hillshade , sky . Except for layers of the background or sky types, each layer must refer to a source.


2 Answers

I found the trick.

just add

map.on('load', function () {
    map.resize();
});

to the js so the map will resize to it's container

like image 144
KSPR Avatar answered Sep 18 '22 13:09

KSPR


This one worked for me. It takes a while before it takes effect but its worth it.

map.on('load', function() {
    $('.mapboxgl-canvas').css('width', '100%');
    $('.mapboxgl-canvas').css('height', '100%');
    map.resize();
});
like image 33
Ebenezer Offei Avatar answered Sep 16 '22 13:09

Ebenezer Offei