我正在尝试绘制圆圈,并在地图上添加弹出窗口,但我一直遇到错误。无法读取未定义的属性(Reading' lng')

发布于 2025-01-25 20:16:19 字数 2299 浏览 3 评论 0原文

  1. 此代码来自我的app.js

  2. 我将其用作地图的默认位置,一旦应用程序打开

    const [mapcenter,setmapcenter] = usestate({lat:34.80746,lng:-40.4796});

  3. 我将其用作 。从“下拉菜单”中选择的地图位置,我从端点获取的数据中获取LAT和LNG

    setMapCenter([[data.countryinfo.lat,data.countryinfo.lng]));

  4. 我将以下道具传递给地图

     
       Zoom = {MapZoom}
       国家 /地区= {mapcountries}
       casestype = {casestype}
       />````
     
  5. 这是我的utils.js文件,根据其案例类型的颜色

     `````import {circle,popup}来自“ react-leflet”;
    
       const casestypecolors = {
       案例:{
         十六进制:“#cc1034”,
         RGB:“ RGB(204,16,52)”,
        Half_op:“ RGBA(204、16、52、0.5)”,
        乘数:800,
         },,
    
       恢复:{
        十六进制:“#7DD71D”,
        RGB:“ RGB(125,215,29)”,
        half_op:“ RGBA(125,215,29,0.5)”,
         乘数:1200,
       },,
         死亡人数: {
        十六进制:“#fb4443”,
        RGB:“ RGB(251,68,67)”,
         Half_op:“ RGBA(251,68,67,0.5)”,
        乘数:2000,
        },,
        };````
     
  6. 这是我的utilis.js。我试图在地图上绘制圆圈和弹出窗口。控制台错误(Untured TypeError:无法读取未定义的属性(读取'lng')

     `````extort const showdataonmap =(data,casestype =“ case”)=> ((
        data.map(((country)=>(( 
        < circle>
        中心= {[country.countryinfo.lat,country.countryinfo.long]}} 
    
          fillopacity = {0.4},
         color = {casestypecolors [casestype] .hex},
         fillcolor = {casestypecolors [casestype] .hex}, 
    
        半径= {
         Math.sqrt(country [Casestype]) * casestypecolors [Casestype] .multiplier
       } 
        < popup>
         < h6>是弹出式工作</h6>
      </popup>
      </circle>
        ))
        );``````
     
  7. 这是从我的map.js. console错误(und typeerror:无法读取未定义的属性(读取'lng')

    >
     ``````import {showdataonmap} from'./utils';
    
            函数映射({中心,Zoom,contife,Casestype}){
                 返回 (
               < div className ='map'>
       < felleTmap Center = {center} Zoom = {Zoom} scrollWheelZoom = {false}>
          < tilelayer
          属性='& copy; < a 
          href =“ https://www.openstreetmap.org/copyright”> openstreetmap</a>贡献者的
             url =“ https:// {s} .tile.openstreetmap.org/{z}/{x}/{y} .png”
            />
             {showdataonmap(国家 /地区,casestype)} 
       </fefletmap>
                  </div>
             )
              }
    
              导出默认地图``
     
  1. This piece of code is from my App.js

  2. I am using this as the default location of the map once the app opens

    const [mapCenter, setMapCenter] = useState({lat: 34.80746, lng: -40.4796});

  3. I use this to update the location of the map, as selected from the dropdown menu, I am getting the lat and lng from data pulled from an endpoint

    setMapCenter([data.countryInfo.lat, data.countryInfo.lng]);

  4. I am passing the following props to the Map

     ```<Map center={mapCenter}
       zoom={mapZoom}
       countries={mapCountries}
       casesType={casesType}
       />```
    
  5. This is from my utils.js file, colors of the said circles according to their case types

     ```import { Circle, Popup } from "react-leaflet";
    
       const casesTypeColors = {
       cases: {
         hex: "#CC1034",
         rgb: "rgb(204, 16, 52)",
        half_op: "rgba(204, 16, 52, 0.5)",
        multiplier: 800,
         },
    
       recovered: {
        hex: "#7dd71d",
        rgb: "rgb(125, 215, 29)",
        half_op: "rgba(125, 215, 29, 0.5)",
         multiplier: 1200,
       },
         deaths: {
        hex: "#fb4443",
        rgb: "rgb(251, 68, 67)",
         half_op: "rgba(251, 68, 67, 0.5)",
        multiplier: 2000,
        },
        };```
    
  6. This is from my utilis.js. I am trying to draw the circle and popup on the map. Console error (Uncaught TypeError: Cannot read properties of undefined (reading 'lng')

      ```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>
         <h6>Is the POPUP working</h6>
      </Popup>
      </Circle>
        ))
        );```
    
  7. This is from my Map.js. Console error (Uncaught TypeError: Cannot read properties of undefined (reading 'lng')

          ```import { showDataOnMap } from './utils';
    
            function Map({center, zoom, countries, casesType}) {
                 return (
               <div className='map'>
       <LeafletMap center={center} zoom={zoom} scrollWheelZoom={false} >
          <TileLayer
          attribution='© <a 
          href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
             url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            />
             {showDataOnMap(countries, casesType)} 
       </LeafletMap>
                  </div>
             )
              }
    
              export default Map```
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆消瘦 2025-02-01 20:16:19

问题在您的showdataonmap函数中,您呈现circle

<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>
    <h6>Is the POPUP working</h6>
  </Popup>
</Circle>

circle的所有属性(中心fillopacity fillopacity ,color等)已定义,您已经使用&gt;关闭了圆圈元素。这应该更改为:

<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
      }
    >

有一个工作demo 在这里 >使圆圈呈现。

The problem is in your showDataOnMap function where you render the Circle:

<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>
    <h6>Is the POPUP working</h6>
  </Popup>
</Circle>

All of the properties of the Circle (center, fillOpacity, color etc) are defined after you've already closed the Circle element with a >. This should be changed to:

<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
      }
    >

There's a working demo here that renders the circles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文