Nat*_*gan 3 google-maps google-maps-api-3 reactjs react-google-maps
我有以下反应组件,它从“react-google-maps”库加载 GoogleMap。我正在关注文档示例,但我从 getBounds() 函数中获得了未定义的信息。我认为这是由于在地图完全加载之前试图获得边界但找不到解决方案。
@inject("map") @observer
export default class Map extends Component {
constructor(props) {
super(props);
}
render() {
const mapStore = this.props.map
const GettingStartedGoogleMap = withGoogleMap(props => (
<GoogleMap
ref={props.onMapLoad}
style={{
height: 100,
width: 100,
}}
defaultOptions={{ styles: this.mapStyles }}
defaultZoom={mapStore.zoom}
defaultCenter={{ lat: mapStore.center.lat, lng: mapStore.center.lng }}>
{
mapStore.markers.map(({ lat, lng }) => {
return <Marker
position={{ lat, lng }}
icon={{
url: require('../../images/marker.png')
}}
/>
})
}
</GoogleMap>
))
return (
<GettingStartedGoogleMap
style={{
height: 100,
width: 100,
}}
onMapLoad={(googleMap) => {
console.log(googleMap.getBounds())
}}
containerElement={
<div style={{ height: 'calc(100vh - 70px)' }
} />
}
mapElement={
<div style={{ height: 'calc(100vh - 70px)' }} />
} />
)
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
小智 5
您可以使用“onIdle”来确保地图已完全准备好
<GoogleMap
ref={props.onMapLoad}
...
onIdle={props.onMapIdle}
>
...
</GoogleMap>
和
<GettingStartedGoogleMap
onMapIdle={ ()=> { console.log('map is ready') } }
...
/>
| 归档时间: |
|
| 查看次数: |
3020 次 |
| 最近记录: |