【发布时间】:2021-11-16 11:05:19
【问题描述】:
问题
我在我的 React 项目中使用 leaflet v1.7.1 和 react-leaflet v3.0.5。
当我尝试React Router's "Setup" documentation page 中的设置示例时,标记图标变成了一个损坏的图像,如下图红色圆圈所示:
根据 React Router 的文档,标记应该是这样的:
经检查,保存标记图像的<img> 标记的src 属性应为https://unpkg.com/leaflet@1.7.1/dist/images/marker-icon-2x.png。然而,经过检查,我的<img> 的src 属性看起来很乱:
复制
我创建了一个包含我的代码的新沙箱:
或者,按照以下步骤复制问题:
-
npx create-react-app leaflet-test -
cd leaflet-test/ -
npm i leaflet react-leaflet -
在代码编辑器中打开项目。转到
App.js并使用以下代码:import React from "react"; import "./App.css"; import "leaflet/dist/leaflet.css"; import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet"; const styles = { mapRoot: { height: 400, }, }; export default function App() { return ( <MapContainer style={styles.mapRoot} center={[51.505, -0.09]} zoom={13} scrollWheelZoom={false} > <TileLayer attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <Marker position={[51.505, -0.09]}> <Popup> A pretty CSS3 popup. <br /> Easily customizable. </Popup> </Marker> </MapContainer> ); } -
npm start
我不确定我是否在 React 中错误地设置了 Leaflet,或者它是来自 Leaflet 或 React Leaflet 的错误。提前致谢!
【问题讨论】:
-
也许这个帖子会对你有所帮助,好消息是这不是你的错,这似乎是一个常见问题github.com/PaulLeCam/react-leaflet/issues/453
-
非常感谢 :D 我从那个问题线程中找到了解决方法。
-
没问题,很高兴您找到了解决方案 :)
标签: javascript reactjs leaflet react-leaflet