我正在尝试绘制圆圈,并在地图上添加弹出窗口,但我一直遇到错误。无法读取未定义的属性(Reading' lng')
此代码来自我的app.js
我将其用作地图的默认位置,一旦应用程序打开
const [mapcenter,setmapcenter] = usestate({lat:34.80746,lng:-40.4796});
我将其用作 。从“下拉菜单”中选择的地图位置,我从端点获取的数据中获取LAT和LNG
setMapCenter([[data.countryinfo.lat,data.countryinfo.lng]));
我将以下道具传递给地图
Zoom = {MapZoom} 国家 /地区= {mapcountries} casestype = {casestype} />````
这是我的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, },, };````
- 这是我的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> )) );``````
这是从我的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> ) } 导出默认地图``
This piece of code is from my App.js
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});
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]);
I am passing the following props to the Map
```<Map center={mapCenter} zoom={mapZoom} countries={mapCountries} casesType={casesType} />```
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, }, };```
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> )) );```
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在您的
showdataonmap
函数中,您呈现circle
:circle的所有属性(
中心
,fillopacity
fillopacity ,color
等)已定义,您已经使用&gt;
关闭了圆圈元素。这应该更改为:有一个工作demo 在这里 >使圆圈呈现。
The problem is in your
showDataOnMap
function where you render theCircle
: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:There's a working demo here that renders the circles.