javascript - How center the new location of Google Map on React? - Stack Overflow

I use google-map-react to display a Google Map with Custom Marker to show a location. Can i change the

I use google-map-react to display a Google Map with Custom Marker to show a location.

Can i change the location succesfully, its mean, passing lat and lng to the map ponent and put the marker on the new location, butthe view of the map its still on the older location.

How can i center on the new location?

Main Component

Lat and Lng starts with a coordinates, whit setState put a new

Don't put the entire ponent but it's the way I call the map

return (
    <div className="main">
        <div className="main__map">
            <TheMap center={[this.state.lat, this.state.lng]} />
        </div>
    </div>
)

TheMap Component

import React,{useState , useEffect} from 'react';
import GoogleMapReact from 'google-map-react';

function TheMap(props){
    const [cordinates , setCordinates] = useState(props);
    const MyCustomMarker = () => <span class="material-icons">place</span>;

    useEffect(() => {
       setCordinates(props);
    }, [props])

    return(
        <GoogleMapReact
            yesIWantToUseGoogleMapApiInternals={true}
            bootstrapURLKeys={{key:'THE KEY'}}
            defaultZoom={16}
            center={cordinates.center}
        >
            <MyCustomMarker
                lat={cordinates.center[0]}
                lng={cordinates.center[1]}
            />
        </GoogleMapReact>
    )
}

export default TheMap

I use google-map-react to display a Google Map with Custom Marker to show a location.

Can i change the location succesfully, its mean, passing lat and lng to the map ponent and put the marker on the new location, butthe view of the map its still on the older location.

How can i center on the new location?

Main Component

Lat and Lng starts with a coordinates, whit setState put a new

Don't put the entire ponent but it's the way I call the map

return (
    <div className="main">
        <div className="main__map">
            <TheMap center={[this.state.lat, this.state.lng]} />
        </div>
    </div>
)

TheMap Component

import React,{useState , useEffect} from 'react';
import GoogleMapReact from 'google-map-react';

function TheMap(props){
    const [cordinates , setCordinates] = useState(props);
    const MyCustomMarker = () => <span class="material-icons">place</span>;

    useEffect(() => {
       setCordinates(props);
    }, [props])

    return(
        <GoogleMapReact
            yesIWantToUseGoogleMapApiInternals={true}
            bootstrapURLKeys={{key:'THE KEY'}}
            defaultZoom={16}
            center={cordinates.center}
        >
            <MyCustomMarker
                lat={cordinates.center[0]}
                lng={cordinates.center[1]}
            />
        </GoogleMapReact>
    )
}

export default TheMap
Share Improve this question asked Jun 7, 2020 at 1:48 Alexis OlveresAlexis Olveres 3011 gold badge4 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You should set the default coordinates to any location by default, for example [10.00, 10.00], then change the dependency array of useEffect to [props.center], finally set the coordinates value to props.center.

The map ponent

import React,{useState , useEffect} from 'react';
import GoogleMapReact from 'google-map-react';

function TheMap(props){
    const [cordinates , setCordinates] = useState([10, 10]);
    const MyCustomMarker = () => <span class="material-icons">place</span>;

    useEffect(() => {
       setCordinates(props.center);
    }, [props.center])

    return(
        <GoogleMapReact
            yesIWantToUseGoogleMapApiInternals={true}
            bootstrapURLKeys={{key:'THE KEY'}}
            defaultZoom={16}
            center={cordinates}
        >
            <MyCustomMarker
                lat={cordinates[0]}
                lng={cordinates[1]}
            />
        </GoogleMapReact>
    )
}

export default TheMap

You need to ensure that in center={coordinates} coordinates is a new object and not only the same object with just changed values of lat and lng. Otherwise react will not recognize it as changed. If it doesn't work in the above then the same object was passed already to props.center.

This happens because you are sending the latitude and longitude as strings, if you inspect the GoogleMapReac element from the reactdevtools and remove the quotes around the latitude and longitude, then it will automatically center the location on the map.

lat and long must be of type Number

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743991664a4539902.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信