简体   繁体   English

反应传单地图无法正常工作

[英]React leaflet map is not working properly

Trying to create a Map using react-leaflet to show data based on Covid-19 worldwide.尝试使用 react-leaflet 创建地图以显示基于 Covid-19 的全球数据。 Fetching the data from API and show them on the map.从 API 获取数据并将其显示在地图上。 The map is not displaying the data in the map rather getting an error like地图未显示地图中的数据,而是出现类似错误

cannot read property 'initialize' of undefined leaflet无法读取未定义传单的属性“初始化”

The map.js where the map is initialized地图被初始化的 map.js

import React from 'react'
import '../componentsstyle/Map.css'
import {Map as MapContainer,TileLayer} from 'react-leaflet'
import { showDataOnMap } from './utils';

function Map({countries,center,zoom,casesType}) {

    return (
        <div className="map">
            <MapContainer center={center} zoom={zoom}>
            <TileLayer
            attribution='&copy; <a href="https://carto.com/">carto.com</a> contributors'         
            url="https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png"
            />
            {showDataOnMap(countries,casesType)}
            </MapContainer>
        </div>
    )
}

export default Map;

The Utils.js is providing necessary functions and the function ShowDataOnMap is used to getting the data from the props and drawing some circles based on the number of cases. Utils.js 提供了必要的功能,函数 ShowDataOnMap 用于从 props 中获取数据并根据案例数量绘制一些圆圈。

Utils.js实用程序.js

import React from 'react'
import numeral from 'numeral'
import { Circle,Popup } from 'leaflet';

const casesTypeColors = {
    cases: {
      hex: "#CC1034",
      mulitiplier: 800,
    },
  
    recovered: {
      hex: "#7DD71D",
      mulitiplier: 1200,
    },
  
    deaths: {
      hex: "#C0C0C0",
      mulitiplier: 2000,
    },
  };


export const sortData=(data)=>{
    const sortedData=[...data]

    sortedData.sort((firstCountry,secondCountry)=>{
        if(firstCountry.cases>secondCountry.cases){
            return -1;
        }

        else{
            return 1;
        }
    })

    return sortedData
}

export const showDataOnMap = (data,casesType="cases") =>

    data.map ((country) => (
      <Circle 
      center={[country.countryInfo.lat,country.countryInfo.long]}
      fillOpacity={0.4}
      color={casesTypeColors[casesType].hex}
      fillColor={casesTypeColors[casesType].hex}
      radius={
        Math.sqrt(country[casesType]) * casesTypeColors[casesType].multiplier
      }
      >

      <Popup>
        <h1>I am a PopUp</h1>
      </Popup>
      </Circle>
  ))

This is where the map component is called.这是调用地图组件的地方。

<Map
   countries={MapCountries}
   center={mapCenter}
   zoom={mapZoom}/>

The MapCountries,mapCenter and mapZoom values are fine. MapCountries、mapCenter 和 mapZoom 值很好。

I'm using react-leaflet@2.8.0 and leaflet@1.7.1.我正在使用 react-leaflet@2.8.0 和 Leaflet@1.7.1。 Faced some difficulties with the recent updated version.最近更新的版本遇到了一些困难。 So, I had to used them.所以,我不得不使用它们。

'Map' is defined in two places. “地图”在两个地方定义。 import {Map as MapContainer,TileLayer} from 'react-leaflet'从'react-leaflet'导入{Map as MapContainer,TileLayer}

and in并在

function Map({countries,center,zoom,casesType}) {功能地图({国家,中心,缩放,casesType}){

Here you are using Mapcontainer instead of alias 'Map'在这里,您使用的是 Mapcontainer 而不是别名“Map”

Just remove the alias 'Map' in the import.只需删除导入中的别名“地图”。

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

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