【发布时间】:2021-08-31 09:16:22
【问题描述】:
所以我使用 google maps API 在我的 react 网页上显示地图,我知道如何通过输入 long 和 lat 来设置初始中心点和标记,但是,现在我正在尝试使用地理编码 API,并将该地理编码推送到地图 api 以将其加载为初始中心点。任何有关如何做到这一点的指导表示赞赏。谢谢你。以下是到目前为止我为 google maps api 提供的代码:
import React, { Component } from "react";
import { Map, GoogleApiWrapper, InfoWindow, Marker } from "google-maps-react";
const mapStyles = {
width: "100%",
height: "30%",
};
export class MapContainer extends Component {
state = {
showingInfoWindow: false, // Hides or shows the InfoWindow
activeMarker: {}, // Shows the active marker upon click
selectedPlace: {}, // Shows the InfoWindow to the selected place upon a marker
};
onMarkerClick = (props, marker, e) =>
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true,
});
onClose = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null,
});
}
};
render() {
return (
<Map
google={this.props.google}
zoom={14}
style={mapStyles}
initialCenter={{
lat: -1.2884,
lng: 36.8233,
}}
>
<Marker
onClick={this.onMarkerClick}
name={"Vendor XYZ's Location"}
/>
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onClose}
>
<div>
<h4>{this.state.selectedPlace.name}</h4>
</div>
</InfoWindow>
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: "apiKEY GOES HERE",
})(MapContainer);
【问题讨论】:
-
似乎这是设置初始中心的反应钩子/生命周期方法/构造函数的情况?
标签: reactjs google-maps google-api geocode