显示带有经纬度、长标记的数据数组:react-leaflet

发布于 2025-01-09 17:04:07 字数 2627 浏览 0 评论 0原文

我有一个简单的 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='&copy; <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 技术交流群。

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

发布评论

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

评论(1

红墙和绿瓦 2025-01-16 17:04:07

您可能只是错过了 map 回调中的 return 语句,或者将其转换为不带大括号的短单表达式箭头函数形式:

        bikes.map((bike) => {
          // Do not forget to `return` something
          return (<Marker 
          key={bike.id}
          position={[bike.location.latitude, bike.location.longitude]}>
            <Popup>
              A pretty CSS3 popup.<br /> Easily customizable.
            </Popup>
          </Marker>);
        })

        // Or a short arrow function that automatically returns the result of a single expression: 
        bikes.map((bike) => ( // No curly braces after the arrow
          <Marker 
          key={bike.id}
          position={[bike.location.latitude, bike.location.longitude]}>
            <Popup>
              A pretty CSS3 popup.<br /> Easily customizable.
            </Popup>
          </Marker>
        ))

请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions 了解有关 JavaScript 中箭头函数语法的更多详细信息。

至于您的浏览器警告,它很可能与您当前的问题无关。

You probably just miss a return statement in your map callback, or to convert it into a short single expression arrow function form without the curly braces:

        bikes.map((bike) => {
          // Do not forget to `return` something
          return (<Marker 
          key={bike.id}
          position={[bike.location.latitude, bike.location.longitude]}>
            <Popup>
              A pretty CSS3 popup.<br /> Easily customizable.
            </Popup>
          </Marker>);
        })

        // Or a short arrow function that automatically returns the result of a single expression: 
        bikes.map((bike) => ( // No curly braces after the arrow
          <Marker 
          key={bike.id}
          position={[bike.location.latitude, bike.location.longitude]}>
            <Popup>
              A pretty CSS3 popup.<br /> Easily customizable.
            </Popup>
          </Marker>
        ))

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.

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