【发布时间】:2017-11-30 20:24:00
【问题描述】:
有人可以帮我看看我的代码中缺少什么吗?我正在尝试这样做几个小时,然后一个又一个错误....
目标是将所有对象坐标提取到 GoogleMaps 标记中...
(reactJS 新手)
class maps extends Component{
constructor(props){
super(props)
this.state = { Objects: [] }
}
render(){
{this.state.Objects.location.coordinates.map((item, i) => (
<Marker position= {{ lat: Objects[i].location.coodinates[0], lng: Objects[i].location.coordinates[1] }}/>
))}
const MyMapComponent = withScriptjs(withGoogleMap((props) =>
<GoogleMap
defaultZoom={2}
defaultCenter={{ lat: 40.6333333, lng: -8.65 }}
>
</GoogleMap>
))
return(
<div>
<MyMapComponent
isMarkerShown
googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyCnQmNcHpzoytzm02Zf-inD6xxTdSRJdLg&v=3.exp&libraries=geometry,drawing,places"
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
/>
</div>
)
}
componentDidMount(){
axios.get('https://api.ost.pt/traffic_events/?key=VMSBvXtmOixXVuaHqeyAxDEhQkbnJpXZBYjDufEu').then( response => {
const { Objects } = response.data
console.log(Objects)
this.setState({Objects})
})
.catch(error => {
console.log('Error fetching and parsing data', error);
});
}
}
These are the objects.coordinates from api data that I need to fetch
任何提示将不胜感激。非常感谢!
【问题讨论】:
-
最初,
Objects是一个空数组。假设这将包含属性location是不公平的。你想达到什么目的? -
如果您是 react 新手,为什么要尝试构建它而不是某个版本的“hello world”?
-
组件第一次渲染iirc时,数据还在从axios加载,所以在尝试访问之前,需要添加一个检查来检查
Objects是否充满了数据,像if (Objects.length)这样通常就足够了。 -
@brandNew 打开这个请。 i.stack.imgur.com/1lfIX.png。我想实现到所有对象的位置和坐标以在地图上创建标记
标签: javascript reactjs google-maps