问题
我在React项目中使用leaflet v1.7.1和react-leaflet v3.0.5。
当我尝试在React Router的“设置”文档页面中尝试设置示例时,标记图标会变成下图所示的红色圆圈中的破损图像:
根据React Router的文档,该标记应如下所示:
经检查,包含标记图像的<img>
标签的src
属性应为https://unpkg.com/leaflet@1.7.1/dist/images/marker-icon-2x.png
。但是,经检查,我的<img>
的src
属性是看起来无意义的乱码:
复制问题
我已创建了一个包含我的代码的新sandbox:
或者,请按照以下步骤复制此问题:
npx create-react-app leaflet-test
cd leaflet-test/
npm i leaflet react-leaflet
Open the project in code editor. Go to
App.js
and use the following code: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的错误。提前致谢!