Closed Emixam23 closed 4 years ago
Call map.resize()
after the container element changes size programmatically.
Hey!
Can you be a bit more specific, please? I added your code by the end of componentDidMount
and I still have the same behavior.
Thanks,
If it's still the same behavior, it means the map container didn't change the size at this moment. The reason I'm not more specific is that this is not a GL JS issue — you use the repo to get help with your application, and this is not a good place to do this. For React questions, I would suggest StackOverflow.
Alright thank @mourner for your feedback, I will then get on SO and if I find any solution, I will post it here
I finally found out..
this.map.once('load', () => {
this.map.resize();
});
For react version:
import React from 'react';
import ReactMapboxGl, {Marker} from 'react-mapbox-gl';
import * as MapboxGl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
export const MapBox = React.memo(function MapBox() {
const Map = ReactMapboxGl({
accessToken: 'Token',
trackResize: true,
});
const onLoaded =(map: MapboxGl.Map) =>{
map.resize();
}
return (
<div className="main-map-container" style={{height: "100%"}}>
<Map
style="mapbox://styles/mapbox/streets-v9"
containerStyle={{
height: "calc(100vh - 130px)",
width: "100%"
}}
center={[19.9449799, 50.06465]}
zoom={[13]}
onStyleLoad={(map)=>onLoaded(map)}
onClick={()=>alert("click")}
>
<Marker coordinates={[19.9449799, 50.0646501]} anchor="bottom">
<div >test</div>
</Marker>
</Map>
</div>
);
});
map.on('idle',function(){
map.resize()
})
try this. it works fine for me.
map.on('idle',function(){ map.resize() })
try this. it works fine for me.
ty, short and true
map.on('idle',function(){ map.resize() })
try this. it works fine for me.
With the above, mapbox will be calling map.resize()
every time the map is on idle
. Not sure if this can have some performance implications.
Maybe using once
will work as well and only calls map.resize()
once:
map.once('idle', function () { map.resize() })
Is it possible to force it to be the correct size right on load? I don't really like the fact that there is still a split second where the map has wrong size.
@vdemcak did you ever find a solution here?
By using the render
event instead of load
or idle
the map should be displayed in the correct size without any weird glitches.
This works for me:
map.current?.once('render',function(){
map.current?.resize()
});
This worked for me:
import React, { useState, lazy, Suspense, useRef, useEffect } from 'react';
import ReactMapGL, { ViewState, MapRef } from 'react-map-gl';
...
const mapRef = useRef<MapRef | null>(null);
useEffect(() => {
if (!displayAsHidden && mapRef.current) {
mapRef.current.getMap().resize();
}
}, [displayAsHidden]);
return (
<ReactMapGL
{...viewport}
ref={mapRef}
style={{ width: '100%', height: '100%' }}
mapStyle={'mapbox://styles/...'}
onMove={(evt): void => setViewport(evt.viewState)}
mapboxAccessToken={mapBoxToken}
onLoad={(): void => setIsLoaded(true)}
>
<Suspense fallback={null}>
<MapDrawControls
drawControls={mapDrawControls}
setGeoJsonData={setGeoJsonData}
geoJsonData={geoJsonData}
isLoaded={isLoaded}
updateGeoJsonCallback={updateInputValueFromMap}
/>
</Suspense>
</ReactMapGL>
Hi !
I saw another question related to my issue but it didn't solve it (maybe because my react approach is wrong, I don't know...)
I have this component:
and its css:
THE ISSUE
While using my component, I want to be able to use it as full screen or in a small container (on the main view). However, in full screen, I have a weird behavior:
RELATED QUESTION: https://github.com/mapbox/mapbox-gl-js/issues/3265
Thanks for any help