我尝试过:
<GoogleMap
defaultZoom={14}
center={{lat: props.mapCenter.lat, lng: props.mapCenter.lng}}
onCenterChanged={getCenter()}
/>
更新:我正在使用无状态功能组件
:
const MarkerClustererExampleGoogleMap = withGoogleMap(props => (
<GoogleMap
defaultZoom={14}
center={{lat: props.mapCenter.lat, lng: props.mapCenter.lng}}
onCenterChanged={getCenter()}
>
)
但我收到错误:
getCenter is undefined.
如何解决这个问题,谢谢。
最佳答案
您可以通过 React 从 map API 调用
到 map 组件getCenter()
、getZoom()
、getBounds()
等函数>ref
这是一个粗略的示例,请注意这是一个未经测试的代码片段,应被视为伪代码,但它希望能让您了解如何进行此设置:
let ref
...
<GoogleMap
ref={(mapRef) => ref = mapRef} // bind the ref
onCenterChanged={() => {
ref.getCenter() // get the center, zoom, whatever using the ref
}}
/>
文档中有有关如何执行此操作的更详细示例,例如 here .
关于google-maps - 如何在react-google-maps中调用函数getCenter()和其他函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43513518/