【发布时间】:2019-11-23 09:12:50
【问题描述】:
我评论了我的代码以明确应该发生什么。我尝试了几个小时从地理编码中创建标记,但是我似乎无法弄清楚如何从承诺中提取值。我摆弄了 useState 和 useEffect,但都没有用。
const GMap = eventList => {
return (
<GoogleMap defaultZoom={10} defaultCenter={{ lat: 51.62919, lng: 7.3928 }}>
{
// Map through location-strings in eventList
eventList.eventList.map(event => {
// Get all geocodes
Geocode.fromAddress(event.city).then(
response => {
const { lat, lng } = response.results[0].geometry.location;
console.log(lat, lng);
},
error => {
console.error(error);
}
);
return (
// Create marker for every geocode (lat/lng pair)
<Marker
key={event._id}
position={{
lat: 51.62919, // How to insert the lat and lng values from response instead?
lng: 7.3928
}}
/>
);
})}
</GoogleMap>
);
};
感谢您的帮助,尽管一开始看起来微不足道,但现在无法解决这个问题真的很令人沮丧。
【问题讨论】:
-
您介意为我们制作一个reproducible example 吗?
标签: reactjs promise maps marker geocode