N
N
Nikolai Eremeev2018-07-02 21:54:43
Google Maps
Nikolai Eremeev, 2018-07-02 21:54:43

I am using the react-google-maps library. How can I get the coordinates of the center of the map?

I am using the react-google-maps library. There is little information on foreign resources. I read that the coordinates can be obtained through getCenter(), but I can't. How to implement a button in the component that will send the center coordinates to the parent function and how to get these center coordinates? Maybe who knows? Thank you!
Here is the component code

const Map = compose(
    withProps({
        googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyB8ECfIjkNyE2AVBlG4Fpd4rD2Y4q5Ytpk",
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px`, }} />,
        mapElement: <div style={{ height: `100%` }} />,
    }),
    lifecycle({
        componentDidMount() {
            const refs = {}

            this.setState({
                onMapMounted: ref => {
                    refs.map = ref;
                    console.log(refs.map.getCenter())
                },
            })
        },
    }),
    withScriptjs,
    withGoogleMap
)((props) =>
    <GoogleMap markers={markers}
        ref={props.onMapMounted}
        defaultZoom={8}
        defaultCenter={{ lat: -34.397, lng: 150.644 }}
    >
        {markers.map( function (obj, index) {
            return (
                <div key={index}>
                    <Marker title="Hello World!" position={{ lat: obj.position.lat, lng: obj.position.lng }} onClick={props.onToggleOpen}>
                        {props.isOpen &&
                            <InfoWindow onCloseClick={props.onToggleOpen}>
                                 <div>
                                     {obj.title}
                                </div>
                            </InfoWindow>
                        }
                    </Marker>
                </div>
            );
        })
        }
        <Polygon path={path} />
    </GoogleMap>
)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question