reactjs - 当我从 node_modules 导入leaflet.css 时未显示传单标记
问题描述
我有这个方法来添加标记到地图
createMarker(map) {
if (this.state.node !== undefined) {
const nodeLength = this.state.node.length;
const latLang = [];
this.state.node.map(item => latLang.push([item.latitude, item.longitude]));
this.marker = [];
// looping node
for (let i = 0; i < nodeLength; i++) {
this.marker[i] = L.marker(latLang[i]).addTo(map).on('click', () => this.handleClick(i));
}
} else {
return 0;
}
}
它在我在 index.html 中使用它时显示
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
但是当我在 index.js 中导入它时,它现在显示
import "leaflet/dist/leaflet.css"
解决方案
这是 webpack 和构建过程中文件捆绑的一个已经存在的问题,请检查#4968。
为了克服这个问题,创建一个带有自定义图标的标记作为 L.icon 实例,如下所示:
....
const icon = L.icon({
iconSize: [25, 41],
iconAnchor: [10, 41],
popupAnchor: [2, -40],
iconUrl: "https://unpkg.com/leaflet@1.6/dist/images/marker-icon.png",
shadowUrl: "https://unpkg.com/leaflet@1.6/dist/images/marker-shadow.png"
});
...
createMarker(map) {
if (this.state.node !== undefined) {
const nodeLength = this.state.node.length;
const latLang = [];
this.state.node.map(item => latLang.push([item.latitude, item.longitude]));
this.marker = [];
// looping node
for (let i = 0; i < nodeLength; i++) {
this.marker[i] = L.marker(latLang[i], { icon }) // here pass icon as an option when creating markers
.addTo(map)
.on('click', () => this.handleClick(i));
}
} else {
return 0;
}
}
推荐阅读
- unity3d - 如何在 Unity 中制作 2d Sprite 3d
- ssis - 在 SSIS ETL 的每一行中添加来自不同源的列
- ios - iOS:以编程方式创建的 UINavigationController 的导航栏未扩展到安全区域
- java - 个人电脑无法访问maven Central
- python-3.x - Pandas:将纳秒时间增量与时间戳相加
- freepascal - 连接到 Oracle DB 时出现 External: SIGSEGV 错误
- ruby - MAC 中的 Ruby 和 Gem
- bash - 在 Keycloak/jboss 上发布启动脚本
- c# - 延迟属性初始化在 C# 中不起作用
- swift - 在@main 上收到 EXC_BAD_ACCESS 错误,并且异常断点不显示失败行