Reactjs 当我点击地图的某个部分时,如何在谷歌地图上放置动态标记?
当我在地图上点击时,我正试图放置标记。react谷歌地图文档不好,我感到困惑Reactjs 当我点击地图的某个部分时,如何在谷歌地图上放置动态标记?,reactjs,google-maps,react-google-maps,Reactjs,Google Maps,React Google Maps,当我在地图上点击时,我正试图放置标记。react谷歌地图文档不好,我感到困惑 class Map extends Component { render() { const GoogleMapExample = withGoogleMap(props => ( <GoogleMap center={{ lat: this.props.latitude, lng: this.props.longitude }} zoom={13}
class Map extends Component {
render() {
const GoogleMapExample = withGoogleMap(props => (
<GoogleMap
center={{ lat: this.props.latitude, lng: this.props.longitude }}
zoom={13}
>
{this.props.markers.map(marker => {
return <SkateMarker key={marker.title} marker={marker} />;
})}
</GoogleMap>
));
return (
<div>
<GoogleMapExample
containerElement={
<div style={{ height: `500px`, width: "1000px" }} />
}
mapElement={<div style={{ height: `100%` }} />}
/>
</div>
);
}
}
export default Map;
类映射扩展组件{
render(){
const GoogleMapExample=withGoogleMap(道具=>(
{this.props.markers.map(marker=>{
返回;
})}
));
返回(
);
}
}
导出默认地图;
以下示例演示如何使用react google maps在地图单击上添加标记:
class MapWithMarkers extends React.Component {
state = {
places: []
};
addMarker(e) {
const newPlace = { id: this.state.places.length, lat: e.latLng.lat(), lng: e.latLng.lng() };
this.setState({
places: [...this.state.places,newPlace]
})
}
render() {
return (
<GoogleMap
onClick={this.addMarker.bind(this)}
defaultZoom={this.props.zoom}
defaultCenter={this.props.center}
>
{this.state.places.map(place => {
return (
<Marker
key={place.id}
position={{ lat: place.lat, lng: place.lng }}
/>
);
})}
</GoogleMap>
);
}
}
类MapWithMarkers扩展了React.Component{
状态={
地点:[]
};
添加标记(e){
const newPlace={id:this.state.places.length,lat:e.latLng.lat(),lng:e.latLng.lng()};
这是我的国家({
地点:[…此.州.地点,新地点]
})
}
render(){
返回(
{this.state.places.map(place=>{
返回(
);
})}
);
}
}