Bug fix: implement onLoad event calling when reuseMaps is set to true (#704)

This commit is contained in:
Joe Jiang 2019-01-28 13:38:23 +08:00 committed by Xiaoji Chen
parent 973bfc4c2c
commit e2f7efcbe2
2 changed files with 24 additions and 7 deletions

View File

@ -19,6 +19,9 @@ export default class BartMap extends Component {
_onViewportChange = viewState => this.setState({viewState});
// eslint-disable-next-line no-alert
_onMapLoad = (event) => alert('Fire MapGL onLoad event');
_renderMarker(station, i) {
const {name, coordinates} = station;
return (
@ -40,6 +43,7 @@ export default class BartMap extends Component {
width="100%"
height="100%"
onViewportChange={this._onViewportChange}
onLoad={this._onMapLoad}
reuseMaps={true}
>

View File

@ -224,7 +224,7 @@ export default class Mapbox {
}
// PRIVATE API
// eslint-disable-next-line max-statements
_create(props: Props) {
// Reuse a saved map, if available
if (props.reuseMaps && Mapbox.savedMap) {
@ -242,13 +242,26 @@ export default class Mapbox {
this._map._container = newContainer;
Mapbox.savedMap = null;
// Update style
if (props.mapStyle) {
this._map.setStyle(props.mapStyle);
}
// Step3: update style and call onload again
const fireLoadEvent = () => props.onLoad({
type: 'load',
target: this._map
});
// TODO - need to call onload again, need to track with Promise?
props.onLoad();
if (props.mapStyle) {
this._map.setStyle(props.mapStyle, {
diff: true
});
// call onload event handler after style fully loaded when style needs update
if (this._map.isStyleLoaded()) {
fireLoadEvent();
} else {
this._map.once('styledata', fireLoadEvent);
}
} else {
fireLoadEvent();
}
} else {
if (props.gl) {
const getContext = HTMLCanvasElement.prototype.getContext;