React-Leaflet marker files not found(未找到 React-Leaflet 标记文件)
问题描述
我有非常简单的代码来使用 react-leaflet 显示地图并在其上放置一个标记.但是,我在浏览器控制台中收到以下两个错误
I've got very simple code to display a map using react-leaflet and place a marker on it. However, i get the following two errors in my browser console
GET http://localhost:8080/marker-icon-2x.png404(未找到)
GET http://localhost:8080/marker-icon-2x.png 404 (Not Found)
GET http://localhost:8080/marker-shadow.png 404(未找到)
GET http://localhost:8080/marker-shadow.png 404 (Not Found)
我尝试通过下载这两个图像并将它们放在根目录来解决此问题.有用.但是,如何更改 react-leaflet 标记元素查找标记图像的 URL?我想将它们存储在./images"而不是根目录中.
I tried to fix this issue by downloading those two images and placing them at the root. It works. However, how can i change the URL the react-leaflet marker element looks for the marker images? I'd like to store them in "./images" rather than at the root.
推荐答案
试试这个:)
由于某种原因,React 传单不包含图像,您需要重新设置默认图标图像.
React leaflet for some reason do not include images and you will need to reset default icons image.
以下是一些工作示例,希望它能解决您的问题.
Below is some working example, I hope it will solve your issue.
您还可以从公共链接之一添加图标
You also can add icons from one of public link
https://cdnjs.com/libraries/Leaflet.awesome-markers
import React, { Component } from 'react';
import L from 'leaflet';
import {
Map, TileLayer, Marker, Popup
} from 'react-leaflet'
import 'leaflet/dist/leaflet.css';
import './style.css';
import icon from 'leaflet/dist/images/marker-icon.png';
import iconShadow from 'leaflet/dist/images/marker-shadow.png';
let DefaultIcon = L.icon({
iconUrl: icon,
shadowUrl: iconShadow
});
L.Marker.prototype.options.icon = DefaultIcon;
这篇关于未找到 React-Leaflet 标记文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未找到 React-Leaflet 标记文件
基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01