简体   繁体   English

单击标记时未显示反应传单弹出窗口

[英]react-leaflet Popup not showing when clicking marker

I'm using react-leaflet in my ReactJS application and I tried to create markers dynamically and added popup as below code.我在我的 ReactJS 应用程序中使用 react-leaflet,我尝试动态创建标记并添加如下代码的弹出窗口。 But popup box is not showing and there is an error appear in web developer console但是没有显示弹出框,并且在 web 开发人员控制台中出现错误

TypeError: point is null类型错误:点是 null

I am using react-leaflet version 2.5.0.我正在使用 react-leaflet 2.5.0 版。 Please kindly advise!请多多指教!

import React, { Component, useState } from 'react';
import { Map as Mapview, TileLayer, Marker, Popup, GeoJSON  } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import {API_URL} from "../../config/config";
import axios from "axios";
import { iconBlue, iconWell } from './icons';

//import { Popover, PopoverBody, PopoverHeader } from 'reactstrap';

class Map extends Component {

    constructor(props) {
        super(props);
        this.state = {
            data : [],
            zoom: 2,
            //modal: false,
        };

        this.getLocationList = this.getLocationList.bind(this);
        //this.setModal = this.setModal.bind(this);
    }

    // setModal(val){
    //     this.setState({modal: val});
    // }

    componentDidMount(){
        this.getLocationList();
    }

    getLocationList(){
        axios.get(API_URL + "location")
            .then((response) => {
            if(response.data.status === "success")
            {
                this.setState({data: response.data.location});
                console.log(this.state.data);
            }
        })
    }

    render() {


        return (
        <div>
            <Mapview 
            style={{ height: "480px", width: "100%" }}
            zoom={6}
            center={[19.745589, 96.15972]}>
                <TileLayer
                    attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
                    url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                />
                {
                    this.state.data.map((location) => 
                    <Marker position={[location.location_lat, location.location_long]} icon={iconWell}>
                        <Popup>A pretty CSS3 popup.<br />Easily customizable.</Popup>
                    </Marker>
                    )
                }
            </Mapview>
        </div>
        );
    }
}

export default Map;

I see no point variable or property in your code, therefore I am not really sure why you get this error.我在您的代码中看不到point变量或属性,因此我不确定您为什么会收到此错误。

So I do not see the error's possible cause if we take into account the code you have posted.因此,如果我们考虑您发布的代码,我看不出错误的可能原因。

Let's assume you fetch successfully the data and you set the state variable data an array of objects.假设您成功获取数据并将 state 变量数据设置为对象数组。 Then your code should look like this in the demo page , of course by including the fetching code using componentDidMount as in your example (here using static data just to illustrate an example)然后你的代码应该在演示页面中看起来像这样,当然通过在你的例子中使用 componentDidMount 包含获取代码(这里使用 static 数据只是为了说明一个例子)

If the data you are fetching is an object and not an array then your iteration here this.state.data.map is not valid.如果您要获取的数据是 object 而不是数组,那么您在此处的迭代this.state.data.map无效。

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

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