Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 750
Nathan Horrigan Avatar asked Sep 12 '26 22:09

Nathan Horrigan


1 Answers

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') } } ... />

like image 71
Max Gram Avatar answered Sep 14 '26 18:09

Max Gram



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!