Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-map-gl <Map> is not fullscreen until resizing window

I am using the ionic framework with React and trying to display a map via mapbox and react-map-gl.

It is working, but the map is only 100px * 100px or so until I resize the page. I have set the width and height css properties to 100vw and 100vh respectively like so:

  <Map
    style={{width: '100vw', height: '100vh'}}
    initialViewState={{
      longitude: -122.4,
      latitude: 37.8,
      zoom: 14
    }}
    mapStyle="mapbox://styles/mapbox/streets-v9"
  >
    <FullscreenControl />
  </Map>

What could cause it to not be 100 % width and height before resizing the page?

like image 910
ni-eminen Avatar asked Nov 02 '25 17:11

ni-eminen


1 Answers

I had the same issue and was able to resolve it with a call to map.resize() in the map component onRender(). Try this:

<Map
    style={{width: '100vw', height: '100vh'}}
    initialViewState={{
        longitude: -122.4,
        latitude: 37.8,
        zoom: 14,
    }}
    mapStyle="mapbox://styles/mapbox/streets-v9"
    onRender={(event) => event.target.resize()}
>
    <FullscreenControl />         
</Map>

event is a MapboxEvent

event.target is a MapboxMap

MapboxMap which is actually a mapboxgl.Map instance has a resize function that can be called.

See react-map-gl API docs: https://visgl.github.io/react-map-gl/docs/api-reference/map#onrender

like image 125
bapin93 Avatar answered Nov 04 '25 06:11

bapin93



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!