Reactjs 如何使用React google maps将google Map React组件重新定位到新地址的中心位置?

Reactjs 如何使用React google maps将google Map React组件重新定位到新地址的中心位置?,reactjs,react-google-maps,Reactjs,React Google Maps,我正在使用react中的“react google maps”库渲染google Map组件。我可以设置一个初始defaultLocation,当贴图最初加载时,它可以正常工作。在文档中,该组件使用了一个“中心”道具,可以将lat和lng传递到该道具,但我无法将其重新渲染/重新居中到新位置。它始终显示默认位置。知道我做错了什么吗 是否有一种方法可以在addAddressComponent类之外使用state,以便动态设置初始defaultCenter,而不是在渲染部分使用道具 import{wi

我正在使用react中的“react google maps”库渲染google Map组件。我可以设置一个初始
defaultLocation
,当贴图最初加载时,它可以正常工作。在文档中,该组件使用了一个“中心”道具,可以将lat和lng传递到该道具,但我无法将其重新渲染/重新居中到新位置。它始终显示默认位置。知道我做错了什么吗

是否有一种方法可以在
addAddressComponent
类之外使用state,以便动态设置初始
defaultCenter
,而不是在渲染部分使用道具

import{withGoogleMap,GoogleMap,Marker}来自'react GoogleMaps';
/*全球谷歌*/
const MapWithMarker=withGoogleMap((道具)=>(
));
类addAddressComponent扩展组件{
建造师(道具){
超级(道具);
此.state={
纬度:0,
液化天然气:0,
};
this.onSuggestSelect=this.onSuggestSelect.bind(this);
}
onSuggestSelect(建议){
控制台日志(建议位置);
此.setState(()=>{
返回{
lat:10.0,
液化天然气:20.022,
};
});
}
render(){
返回(
);
}
}
导出默认的addAddressComponent;

您需要将
center
属性传递给
GoogleMap
组件中的
MapWithMarker
组件。另外,中心参数必须是如下所示的对象:

{拉丁美洲:-34.397,液化天然气:150.644}

以下示例允许您使用按钮更改地图的中心:

import logo from './logo.svg';
import './App.css';
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps';
import {useState} from "react";
const MyMapComponent = withScriptjs(withGoogleMap((props) =>
    <GoogleMap
        defaultZoom={13}
        center={props.center}
        defaultCenter={{ lat: -34.397, lng: 150.644 }}
    >
      {props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
    </GoogleMap>
))
function App() {
  const [position, setPosition] = useState({ lat: -34.397, lng: 150.644 });
  return (
    <div className="App">
      <button onClick={() => setPosition({lat: 30, lng: 10})}>,
        Click me
      </button>
      <MyMapComponent
          isMarkerShown
          googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places"
          loadingElement={<div style={{ height: `100%` }} />}
          containerElement={<div style={{ height: `100%` }} />}
          center={position}
          mapElement={<div style={{ height: `1000px`}} />}
      />
    </div>
  );
}
export default App;
从“/logo.svg”导入徽标;
导入“/App.css”;
从“react GoogleMaps”导入{withScriptjs,withGoogleMap,GoogleMap,Marker};
从“react”导入{useState};
const MyMapComponent=withScriptjs(withGoogleMap((道具)=>
{props.ismarkersown&}
))
函数App(){
const[position,setPosition]=useState({lat:-34.397,lng:150.644});
返回(
设置位置({lat:30,lng:10}>,
点击我
);
}
导出默认应用程序;

您需要将
center
属性传递给
GoogleMap
组件中的
MapWithMarker
组件。另外,中心参数必须是如下所示的对象:

{拉丁美洲:-34.397,液化天然气:150.644}

以下示例允许您使用按钮更改地图的中心:

import logo from './logo.svg';
import './App.css';
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from 'react-google-maps';
import {useState} from "react";
const MyMapComponent = withScriptjs(withGoogleMap((props) =>
    <GoogleMap
        defaultZoom={13}
        center={props.center}
        defaultCenter={{ lat: -34.397, lng: 150.644 }}
    >
      {props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
    </GoogleMap>
))
function App() {
  const [position, setPosition] = useState({ lat: -34.397, lng: 150.644 });
  return (
    <div className="App">
      <button onClick={() => setPosition({lat: 30, lng: 10})}>,
        Click me
      </button>
      <MyMapComponent
          isMarkerShown
          googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places"
          loadingElement={<div style={{ height: `100%` }} />}
          containerElement={<div style={{ height: `100%` }} />}
          center={position}
          mapElement={<div style={{ height: `1000px`}} />}
      />
    </div>
  );
}
export default App;
从“/logo.svg”导入徽标;
导入“/App.css”;
从“react GoogleMaps”导入{withScriptjs,withGoogleMap,GoogleMap,Marker};
从“react”导入{useState};
const MyMapComponent=withScriptjs(withGoogleMap((道具)=>
{props.ismarkersown&}
))
函数App(){
const[position,setPosition]=useState({lat:-34.397,lng:150.644});
返回(
设置位置({lat:30,lng:10}>,
点击我
);
}
导出默认应用程序;