Nathan Horrigan
Nathan Horrigan

Reputation: 813

react-google-maps : Can't get bounds

I have the following react component that loads a GoogleMap from 'react-google-maps' library. I'm following the documentation examples but I get undefined from getBounds() function. I think it is due to trying to get the bounds before map is fully loaded but can't find a solution.

@inject("map") @observer
export default class Map extends Component {

    constructor(props) {
        super(props);
    }

    render() {
        const mapStore = this.props.map
        const GettingStartedGoogleMap = withGoogleMap(props => (
            <GoogleMap
                ref={props.onMapLoad}
                style={{
                    height: 100,
                    width: 100,
                }}
                defaultOptions={{ styles: this.mapStyles }}
                defaultZoom={mapStore.zoom}
                defaultCenter={{ lat: mapStore.center.lat, lng: mapStore.center.lng }}>
                {
                    mapStore.markers.map(({ lat, lng }) => {
                        return <Marker
                            position={{ lat, lng }}
                            icon={{
                                url: require('../../images/marker.png')
                            }}
                        />
                    })
                }
            </GoogleMap>
        ))

        return (
            <GettingStartedGoogleMap
                style={{
                    height: 100,
                    width: 100,
                }}
                onMapLoad={(googleMap) => {
                    console.log(googleMap.getBounds())
                }}
                containerElement={
                    <div style={{ height: 'calc(100vh - 70px)' }
                    } />
                }
                mapElement={
                    <div style={{ height: 'calc(100vh - 70px)' }} />
                } />
        )
    }
}

Thanks in advance

Upvotes: 3

Views: 3710

Answers (1)

Max Gram
Max Gram

Reputation: 695

you can use 'onIdle' to make sure map is fully ready

<GoogleMap ref={props.onMapLoad} ... onIdle={props.onMapIdle} > ... </GoogleMap>

and

<GettingStartedGoogleMap onMapIdle={ ()=> { console.log('map is ready') } } ... />

Upvotes: 5

Related Questions