Skip to content

Commit

Permalink
Handle initialViewState.bounds when reusing map (#1923)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Jul 21, 2022
1 parent d3c409e commit bcf8d2f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,16 @@ export default class Mapbox {
map._container = container;

// Step 3: apply new props
if (props.initialViewState) {
that._updateViewState(props.initialViewState, false);
}
map.resize();
that.setProps({...props, styleDiffing: false});
map.resize();
const {initialViewState} = props;
if (initialViewState) {
if (initialViewState.bounds) {
map.fitBounds(initialViewState.bounds, {...initialViewState.fitBoundsOptions, duration: 0});
} else {
that._updateViewState(initialViewState, false);
}
}

// Simulate load event
if (map.isStyleLoaded()) {
Expand Down
11 changes: 6 additions & 5 deletions test/apps/reuse-maps/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as React from 'react';
import {useState} from 'react';
import {render} from 'react-dom';
import Map from 'react-map-gl';
import Map, {MapProps} from 'react-map-gl';

const TOKEN = ''; // Set your mapbox token here

const CONFIGS = [
const CONFIGS: MapProps[] = [
{
style: {width: '100%', height: '100%'},
mapStyle: 'mapbox://styles/mapbox/dark-v9',
Expand All @@ -19,9 +19,10 @@ const CONFIGS = [
style: {width: 400, height: 300, margin: 100},
mapStyle: 'mapbox://styles/mapbox/light-v9',
initialViewState: {
longitude: -100,
latitude: 40,
zoom: 3.5
bounds: [
[-125, 35],
[-70, 45]
]
}
},
{
Expand Down

0 comments on commit bcf8d2f

Please sign in to comment.