javascript - ReactJS 编译失败 : 'Objects' is not defined no-undef

标签 javascript reactjs google-maps

有人可以帮我看看我的代码中缺少什么吗?我尝试这样做几个小时,然后错误接连出现......

目标是将所有对象坐标提取到 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

任何提示将不胜感激。非常感谢!

最佳答案

这是错误的:

{this.state.Objects.location.coordinates.map((item, i) => (
    <Marker position= {{ lat: Objects[i].location.coodinates[0], lng: Objects[i].location.coordinates[1] }}/>
))}

因为您正在映射 this.state.Objects.location.coordinates,并且您正在使用索引访问 Objects[i].location.coordinates[0]Objects[ i].location.coordinates[1],并且 Objects 未在任何地方定义,但 this.state.Objects 是。

我猜你的意思是这样的:

 {this.state.Objects.map((item, i) => (
        <Marker position= {{ lat: item.location.coodinates[0], lng: item.location.coordinates[1] }}/>
    ))}

这是一个如何列出每个对象的纬度/经度的示例:http://jsfiddle.net/g4hr5jnc/

您只需将其替换为 Google map 组件

关于javascript - ReactJS 编译失败 : 'Objects' is not defined no-undef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47581967/

相关文章:

javascript - CSS3 从固定到绝对的过渡

javascript - 将带有枚举的属性传递给需要带有字符串的属性的函数

javascript - 为什么没有 [] 的对象键不会成为可接受的参数?

javascript - 如何在 Google map 中添加我的位置按钮?

javascript - 如何从谷歌地图 v3 api 获取纬度和经度?

google-maps - 计算谷歌地图图 block 边界坐标

javascript - 动态填充 Bootstrap 选择器 : change event doesn't work

javascript - 使用map从数组的数组创建新数组的更好方法

node.js - 如何从 mongoDB 获取多个文档?

css - React 样式/css/sass 顺序