【发布时间】:2020-01-26 00:07:18
【问题描述】:
我正在使用地震 api 通过“react-google-maps”(Doc) 获取此 JsonData。 但不知何故,标记没有出现,我不明白为什么。
const url =
"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson";
const Map = () => {
const defaultCoordinate = { lat: -0.8962, lng: -91.4445 };
const getMarkers = () => {
FetchEarthquakeData(url).then(result => {
console.log(result.features); //LOG1
result.features.map(earthquake => (
console.log(earthquake), //LOG2
<Marker
key={earthquake.id}
position={{
lat: earthquake.geometry.coordinates[1],
lng: earthquake.geometry.coordinates[0]
}}
/>
));
});
};
return (
<GoogleMap defaultCenter={defaultCoordinate} defaultZoom={8}>
{getMarkers()}
</GoogleMap>
);
};
const WrappedMap = withScriptjs(withGoogleMap(Map));
第一个 console.log (LOG1) 从 JsonData 返回数组。 LOG 2 返回数组的每个对象。控制台中没有错误。
【问题讨论】:
-
删除LOG2和逗号就可以了
-
getMarkers没有返回任何内容,即使它返回了,您也正在尝试同步获取异步调用的结果。
标签: javascript reactjs asynchronous google-maps-api-3