反应,错误显示传单(地图)Foreach模式

时间:2019-05-10 02:19:33

标签: reactjs google-maps google-maps-api-3 leaflet react-leaflet

我的问题实际上是在模态图内部显示地图,但是当我循环访问每个数据以显示它们时,出现错误:

  

错误:找不到地图容器。

那太可怕了,因为那使我整整一天都没有任何结果。

包含(地图)的类:

import React, { Component } from 'react'
import {
  Modal, Form, Input, Label, message, Tooltip, Icon, Cascader, Select, Row, Col, Checkbox, Button, AutoComplete,
} from 'antd';
import 'antd/dist/antd.css';
import L from 'leaflet'
import 'leaflet/dist/leaflet.css'


export default class MoreInfo extends Component {

  state = {
    visible: false
  }

  showModal = () => {
    this.setState({
      visible: true,
    });
  }

  handleOk = (e) => {
    console.log(e);
    this.setState({
      visible: false,
    });
  }

  handleCancel = (e) => {
    console.log(e);
    this.setState({
      visible: false,
    });
  }

  /****************************** */


 
  handlePositionMap = () => {
    const center = ["34.0389", "-6.8166"];

    let map2 = document.querySelector('#map');

    map2 = L.map('map').setView(center, 13);

    let token = 'pk.eyJ1IjoidnVkaWRlIiwiYSI6ImNqdWw0bmVlNDF3eGs0MW9hYjlyNWI1d3gifQ.BF_c4537GAPelCGeJwMqCg'
    let icon = L.icon({
        iconUrl: 'https://unpkg.com/leaflet@1.4.0/dist/images/marker-icon.png',
    });
    L.tileLayer(`https://api.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.png?access_token=${token}`, {
        maxZoom: 18,
        minZoom: 7,
        attribution: '© <a href="#">Golan</a>'
    }).addTo(map2);
    L.marker(center, {icon: icon}).addTo(map2);

  }

  componentDidMount(){
    this.handlePositionMap();
  }



  render() {
    return (
      <div>

        <Button type="primary" onClick={this.showModal}>
          More
        </Button>
        <Modal
          title="More Info"
          visible={this.state.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
        >



        <Form.Item label="Location" >
          <div id="map" style={{"width": "206px","height": "113.8px"}} ></div>
        </Form.Item>


          <p>Some contents...</p>
          <p>Some contents...</p>
          <p>Some contents...</p>
        </Modal>
      </div>


    )
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>

第二,哪里叫:

class Place extends Component {

  constructor(props){
    super(props);


    this.state = {
      place: this.props.place,
      locals: this.props.locals,
      firstLoad: 0,
      cartItems: [],
      buttonName: "",
    }
  }


  componentDidMount(){
    console.log("[Place] -> componentDidMount() : ");
    console.log(this.state.place);

    if(localStorage.getItem("cartItems")){
      this.setState({ cartItems: JSON.parse(localStorage.getItem("cartItems")) });
    }
    //this.handlePositionMap();
    console.log("-----------------------------------");
  }

 

  render(h) {

    console.log("Place :", this.props.place);

    return (

    <div>


      { this.props.place ? (

            <Card key={this.props.place.id }  >
                <CardMedia
                    style={{height: 0, paddingTop: '55.25%'}}
                    image="https://s3-eu-west-1.amazonaws.com/parkings.bepark.eu/488/5ab8e253a27cb4.58776216.jpeg"
                    title="test"

                />


        
                <CardContent>
                    <Typography gutterBottom variant="headline" component="h2">
                        { this.capitalize(this.props.place.local_name) }
                    </Typography>
                    <Typography component="p">
                        { this.capitalize(this.props.place.local_address) }
                    </Typography>
                </CardContent>



                <CardFooter>

                <CardActions>


                    <MoreInfo
                      place={this.props.place}
                      locals={this.props.locals}
                    />

                    &nbsp;

                    <Modal
                        modal_name="Edit"
                        place={this.props.place}
                        locals={this.props.locals}
                        handleEdited={this.props.handleEdited}
                        handleLoadPlace={this.props.handleLoadPlace}
                    />

                    <Popconfirm title="Are you sure?" onConfirm={() => this.handlClickDelete(this.props.place.id)} okText="Yes" cancelText="No">
                      <Button size="small" variant="contained" color="secondary" >
                          Delete
                      </Button>
                    </Popconfirm>

                </CardActions>

                   

                </CardFooter>


            </Card>



        ): null


        }
      </div>
    );
  }

}

export default Place;

生成所有地点的类:

/ ...

            { (Array.isArray(this.state.places) && this.state.places.length) ? (this.state.places.map( place =>  (
                <Grid item xs={12} sm={3} lg={4} xl={3} key={place.id} >
                    <Place
                        place={place}
                        locals={this.state.locals}
                        handleDelete={this.handleDelete}
                        handleLoadPlace={this.handleLoadPlace}
                        handleEdited={this.handleEdited}
                        handleAddToCart={ this.test.bind(this) }
                        handleCartPlaceExists={this.handleExistPlace.bind(this)}
                    />
                </Grid>
            ))): "No Places found" }


/...

结果:

https://i.stack.imgur.com/Cr4Gq.png

  

您能帮我找到解决方案吗,我期待您的回答,谢谢^ _ ^

1 个答案:

答案 0 :(得分:0)

由于错误提示示例{em>中缺少地图容器documentation

  

根据给定的<div>元素的DOM ID,实例化地图对象

这是一个简单的例子:

class Map extends Component {

  componentDidMount(){
    const map = L.map('map').setView([51.505, -0.09], 13);

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
            '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
        id: 'mapbox.streets'
    }).addTo(map);
  }


  render() {
    return (
        <div id="map"/>
    );
  }
}

Demo