google-maps - 如何在react-google-maps中调用函数getCenter()和其他函数

标签 google-maps reactjs

我尝试过:

<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 调用 getCenter()getZoom()getBounds() 等函数>ref 到 map 组件

这是一个粗略的示例,请注意这是一个未经测试的代码片段,应被视为伪代码,但它希望能让您了解如何进行此设置:

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/

相关文章:

javascript - React JS onClick 打开当前下拉菜单

javascript - 与 child 一起 react 测试组件

css - Reactjs 和 Material UI 上的悬停功能

javascript - 在表中单击时调用方法并传递链接值?

google-maps - 如何在 Windows Phone 8 应用程序中添加谷歌地图

javascript - 我们能查出谷歌地图是否已经初始化了吗?

javascript - 在函数调用时重绘 Google map 圆圈和标记

javascript - 在循环点中在 Canvas 上绘图已关闭

ios - 如何在 Google Map iOS SDK 中处理多种类型的聚类

javascript - Reactjs app div onclick 在 IE 11 中不起作用,没有调用任何函数