用lat显示数据数组,只要标记:react-leaflet
Displaying array of data with lat, long as Markers: react-leaflet
我有一个简单的 React 应用程序,它使用 Leaflet 和 OpenStreetMap 在地图上显示自行车站点。
我已经获取了所有纬度和经度,但是当我映射数组并创建 <Marker key={bike.id} position={[lat, long]}><Marker/>
组件时,它们没有出现在地图上。
相反,我在浏览器控制台上收到此 警告:
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.
我看到一些类似的问题和答案。这是因为 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
您可能只是错过了 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>
))
有关 JavaScript 中箭头函数语法的更多详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions。
至于您的浏览器警告,很可能与您当前的问题无关。
我有一个简单的 React 应用程序,它使用 Leaflet 和 OpenStreetMap 在地图上显示自行车站点。
我已经获取了所有纬度和经度,但是当我映射数组并创建 <Marker key={bike.id} position={[lat, long]}><Marker/>
组件时,它们没有出现在地图上。
相反,我在浏览器控制台上收到此 警告:
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.
我看到一些类似的问题和答案。这是因为 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
您可能只是错过了 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>
))
有关 JavaScript 中箭头函数语法的更多详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions。
至于您的浏览器警告,很可能与您当前的问题无关。