【发布时间】:2019-08-11 07:26:00
【问题描述】:
我正在使用“google-maps-react”并尝试通过点击向我的地图添加新标记。
我目前设法通过控制台记录特定的 latLng,但似乎无法创建新的。我对 React 很陌生。
我的 onMapClick 用于查找纬度和经度。但我认为我需要将位置添加到数组中,然后使用该位置来更新地图。可能是错的
onMapClick = (map,maps,e) => {
const { latLng } = e;
const latitude = e.latLng.lat();
const longitude = e.latLng.lng();
console.log(latitude + ", " + longitude);
var marker = new window.google.maps.Marker({
position: e.latLng,
setMap: map,
});
}
我目前的解决方案是我只是在我的 render() 中用 Marker 中数组的位置硬编码了一些标记
<Marker
onClick={this.onMarkerClick}
name={storedLocations[0]}
position={{lat:listLatitude[0], lan:listLongitude[0]}}
/>
我的信息窗口是:
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onClose}
>
<div>
<h4>{this.state.selectedPlace.name}</h4>
</div>
</InfoWindow>
</Map>
【问题讨论】:
标签: reactjs google-maps