显示带有经纬度、长标记的数据数组:react-leaflet
我有一个简单的 React 应用程序,它使用 Leaflet 和 OpenStreetMap 在地图上显示自行车站。
我已获取所有纬度和经度,但是当我映射数组并创建
组件,它们没有出现在地图上。
相反,我在浏览器控制台上收到此警告:
会变内存消耗太高。预算限制是文档表面积乘以 3(432150 像素)。超出预算的 will-change 的发生将被忽略。
我看到一些类似的问题和答案。这是因为CSS中出现了will-change,但我正在使用Leaflet库本身,所以我不知道在哪里解决这个问题。我也在 YouTube 上看过一些类似的视频,即使我们的逻辑几乎相同,他们也没有任何问题。
MapComponent.jsx
import React, { useState } from 'react';
import '../styles/MapComponent.css';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import { useEffect } from 'react';
import { fetchUserData, fetchNetworks } from '../redux/action/';
import { useDispatch, useSelector } from 'react-redux'
const MapComponent = () => {
const dispatch = useDispatch()
const [latitude, setLatitude] = useState(0)
const [longitude, setLongitude] = useState(0)
const [checkCords, setCheckCords] = useState(false)
const countryCode = useSelector((state) => state.userData.country_code)
const bikeNetworks = useSelector((state) => state.bikeNetworks.networks)
const bikes = bikeNetworks.filter((network) => network.location.country == countryCode)
useEffect(() => {
if(navigator.geolocation) {
navigator.geolocation.watchPosition((position) => {
setLatitude(position.coords.latitude)
setLongitude(position.coords.longitude)
setCheckCords(true)
})
}
}, [])
useEffect(() => {
dispatch(fetchUserData())
dispatch(fetchNetworks())
}, [])
return (
!checkCords ? <h1>Loading...</h1> :
<MapContainer center={[latitude, longitude]} zoom={11}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={[latitude, longitude]}>
<Popup>
A pretty CSS3 popup.<br /> Easily customizable.
</Popup>
</Marker>
{
bikes.map((bike) => {
<Marker
key={bike.id}
position={[bike.location.latitude, bike.location.longitude]}>
<Popup>
A pretty CSS3 popup.<br /> Easily customizable.
</Popup>
</Marker>
})
}
</MapContainer>
)
}
export default MapComponent
I have a simple React app that shows bike stations on a map with Leaflet and OpenStreetMap.
I have fetched all lat and long but when I map though the array and create <Marker key={bike.id} position={[lat, long]}><Marker/>
component, they are not appearing on map.
Instead I have this warning on browser console:
Will-change memory consumption is too high. Budget limit is the document surface area multiplied by 3 (432150 px). Occurrences of will-change over the budget will be ignored.
I see some similar questions and answers to this. It is because occurences of will-change in CSS, but I am using Leaflet library itself, so I do not know where to fix this. I also watch some similar videos on YouTube, even if we have almost same logic they did not have any problem.
MapComponent.jsx
import React, { useState } from 'react';
import '../styles/MapComponent.css';
import { MapContainer, TileLayer, Marker, Popup } from 'react-leaflet';
import { useEffect } from 'react';
import { fetchUserData, fetchNetworks } from '../redux/action/';
import { useDispatch, useSelector } from 'react-redux'
const MapComponent = () => {
const dispatch = useDispatch()
const [latitude, setLatitude] = useState(0)
const [longitude, setLongitude] = useState(0)
const [checkCords, setCheckCords] = useState(false)
const countryCode = useSelector((state) => state.userData.country_code)
const bikeNetworks = useSelector((state) => state.bikeNetworks.networks)
const bikes = bikeNetworks.filter((network) => network.location.country == countryCode)
useEffect(() => {
if(navigator.geolocation) {
navigator.geolocation.watchPosition((position) => {
setLatitude(position.coords.latitude)
setLongitude(position.coords.longitude)
setCheckCords(true)
})
}
}, [])
useEffect(() => {
dispatch(fetchUserData())
dispatch(fetchNetworks())
}, [])
return (
!checkCords ? <h1>Loading...</h1> :
<MapContainer center={[latitude, longitude]} zoom={11}>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
<Marker position={[latitude, longitude]}>
<Popup>
A pretty CSS3 popup.<br /> Easily customizable.
</Popup>
</Marker>
{
bikes.map((bike) => {
<Marker
key={bike.id}
position={[bike.location.latitude, bike.location.longitude]}>
<Popup>
A pretty CSS3 popup.<br /> Easily customizable.
</Popup>
</Marker>
})
}
</MapContainer>
)
}
export default MapComponent
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能只是错过了
map
回调中的return
语句,或者将其转换为不带大括号的短单表达式箭头函数形式:请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions 了解有关 JavaScript 中箭头函数语法的更多详细信息。
至于您的浏览器警告,它很可能与您当前的问题无关。
You probably just miss a
return
statement in yourmap
callback, or to convert it into a short single expression arrow function form without the curly braces:See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions for more details about the syntax of arrow functions in JavaScript.
As for your browser warning, it is very probably not related to your current issue.