简体   繁体   English

数组未映射

[英]Array is not mapping

When this code loads all I see is a blank map. 当此代码加载时,我看到的是空白地图。 The markers (child component) are not rendering. 标记(子组件)未渲染。 I was trying to create a filter for the buttons as well but lets ignore for now until we can get the markers to appear. 我也尝试为按钮创建过滤器,但现在让我们忽略它,直到我们可以显示标记为止。 Any input would be appreciated! 任何输入将不胜感激!

import React from 'react';
import {GoogleApiWrapper, Map} from 'google-maps-react';
import Marker from '../marker.js';
import { Button } from 'react-bootstrap';



class Layout extends React.Component {
  constructor(props){
    super(props)
      this.state = {
        setFilter: ''
      }
  }

  setFilter(event) {
     this.setState({ selectedFilter: event.target.name });
  }

  render () {
    let incidents = [{lat: 32.575258, lng: -117.061613, incident_type: 'ems', icon:'https://s3-us-west-1.amazonaws.com/et-icons/icon_report_ems.png'},
          {lat: 32.958337, lng: -117.096112, incident_type: 'fire', icon:'https://s3-us-west-1.amazonaws.com/et-icons/icon_report_fire.png'},
          {lat: 32.728588, lng: -117.100064, incident_type: 'hazmat', icon:'https://s3-us-west-1.amazonaws.com/et-icons/icon_report_hazmat.png'},
          {lat: 32.556325, lng: -117.055856, incident_type: 'mva', icon:'http://s3-us-west-1.amazonaws.com/et-icons/icon_report_mva.png'},
          {lat: 32.691563, lng: -117.072024, incident_type: 'fire', icon:'https://s3-us-west-1.amazonaws.com/et-icons/icon_report_fire.png'},
          {lat: 32.805941, lng: -117.219577, incident_type: 'ems', icon:'https://s3-us-west-1.amazonaws.com/et-icons/icon_report_ems.png' },
          {lat: 32.717516, lng: -117.164727, incident_type: 'hazmat', icon:'https://s3-us-west-1.amazonaws.com/et-icons/icon_report_hazmat.png'},
          {lat: 32.715218, lng: -117.160156, incident_type: 'mva', icon:'http://s3-us-west-1.amazonaws.com/et-icons/icon_report_mva.png'}]
    return (
      <div>
        <div>
           <Button bsStyle="primary" bsSize="sm" active>EMS</Button>
           <Button bsStyle="danger" bsSize="sm" active>FIRE</Button>
           <Button bsStyle="warning" bsSize="sm" active>HAZMAT</Button>
           <Button bsStyle="info" bsSize="sm" active onClick={this.state.setFilter} name="MVA">MVA</Button>
        </div>
          <div ref="map">
            <Map google={this.props.google}
              style={{width: '100%', height: '100%', position: 'relative'}}
              className={'map'}
              zoom={10}
              initialCenter={{lat: 32.7157, lng: -117.1611}}>
              {incidents.filter((i) => i.incident_type === this.state.selectedFilter).map(i => {
             <Marker
               incident_type={i.incident_type}
               position={{lat: i.lat, lng: i.lng}}
               icon={i.icon} />
             })}
            </Map>
          </div>
      </div>
    )
  }
}

export default GoogleApiWrapper({
  apiKey: 'AIzaSyB0P-Ql1Gdvu0baPK4xmQMchXxQoUk4YH8'
})(Layout);

On load, this.state.selectedFilter is undefined . 加载时, this.state.selectedFilterundefined As a result, incidents.filter((i) => i.incident_type === this.state.selectedFilter) return a blank array and no markers are generated. 结果, incidents.filter((i) => i.incident_type === this.state.selectedFilter)返回一个空白数组,并且不生成任何标记。

You can init selectedFilter to see at least one marker: 您可以初始化selectedFilter来查看至少一个标记:

class Layout extends React.Component {
  constructor(props){
    super(props)
      this.state = {
        setFilter: '',
        selectedFilter: 'fire'
      }
  }
  ...
}

Or you can remove the filter to see if you see the markers: 或者,您可以删除过滤器以查看是否看到标记:

<div ref="map">
    <Map google={this.props.google}
        style={{ width: '100%', height: '100%', position: 'relative' }}
        className={'map'}
        zoom={10}
        initialCenter={{ lat: 32.7157, lng: -117.1611 }}>
        {incidents.map(i => {
            <Marker
                incident_type={i.incident_type}
                position={{ lat: i.lat, lng: i.lng }}
                icon={i.icon} />
        })}
    </Map>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM