我有以下回报:
import {Map, Marker} from 'google-maps-react';
class SimpleMap extends Component{
constructor(props) {
super(props);
this.state = {
marker: {
available: false,
position: {
lat: lathere,
lng: lnghere
}
}
};
}
onClick = (t, map, coord) => {
const {latLng} = coord;
let {marker} = this.state;
marker.position.lat = latLng.lat();
marker.position.lng = latLng.lng();
this.setState({marker});
};
render(){
const {marker} = this.state;
return (<Map
google={this.props.google}
zoom={8}
initialCenter={{position here}}
onClick={this.onClick}>
<Marker position={marker.position}/>
</Map>);
}}
当我再次在地图上单击时,如何删除当前标记?我已经搜索了一些解决方案,但是在每个解决方案中,都有以new google.maps.map
和new google.maps.marker
实例化的对象另存为全局变量,并且相互之间使用引用,以便可以删除标记的地图。我该如何在ReactJS中做到这一点?
感谢HMehmood1的回答here。您只需要在lat
中显式添加lng
和<Marker>
。例如:
<Marker position={{ lat: marker.position.lat, lng: marker.position.lng }}/>