首页 > 解决方案 > 如何确保地图已加载 google-map-react

问题描述

我正在使用google-map-react并且需要访问map.

我通过执行以下操作访问 ref:

<GoogleMap ref={ref => this.map = ref} />

当我componentDidMount()像这样调用 ref 时,this.map.map_它返回 null 因为地图尚未加载。我需要一种在组件安装时访问它的方法。

标签: reactjsgoogle-map-react

解决方案


我认为您可以使用onGoogleApiLoaded

handleGoogleApiLoaded = ({map, maps}) => {
  map...
}

<GoogleMap  
  onGoogleApiLoaded={this.handleGoogleApiLoaded}
  yesIWantToUseGoogleMapApiInternals
/>

推荐阅读