gpt4 book ai didi

Leaflet 自定义图标,react-leaflet,无法构建 Gatsby 项目

转载 作者:行者123 更新时间:2023-12-01 21:45:34 25 4
gpt4 key购买 nike

当我运行 gatsby develop 时,我的 Leaflet map 中的自定义图标工作正常在本地,但是当我尝试构建时,出现与我的自定义标记图标相关的错误:

 WebpackError: TypeError: leaflet_src_layer_marker__WEBPACK_IMPORTED_MODULE_4__.Icon is not a constructor

相关进口:

import { Map, Marker, Popup, TileLayer, Tooltip, ZoomControl } from 'react-leaflet'
import { Icon } from 'leaflet/src/layer/marker'

创建自定义图标:

// Init custom map icon
const mapIcon = MapIcon();
const markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/leaflet@1.5.1/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});

JSX:

{ markerData.length > 0 ? markerData.map((node, index) => (
<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={markerIcon}>
<Popup className="leaflet-popup" onOpen={(el) => openPopup(el)}>
<h5 className="popup-location-title">{node.location_title}</h5>
<h6 className="popup-address">{node.address}</h6>
<div className="popup-description" dangerouslySetInnerHTML={{ __html: node.description }}></div>
{!!node.embed ?
<div className="popup-embed">
<Embed url={node.embed} className="popup-media-embed" />
</div>
: null}
</Popup>
<Tooltip className="leaflet-tooltip">
<span className="title">{node.location_title}</span>
<span className="address">{node.address}</span>
</Tooltip>
</Marker>
)) : null }

我已经尝试了此处列出的所有解决方案:https://github.com/Leaflet/Leaflet.markercluster/issues/874 .我也看过几个类似的问题。似乎没有任何帮助。例如,我也尝试像这样导入传单 import * as L from "leaflet"像这样import L from "leaflet" .然后像这样创建新图标const markerIcon = L.Icon({ ,像这样const markerIcon = L.icon({ ,像这样const markerIcon = new L.Icon({ ,像这样const markerIcon = new L.icon({ .我可以console.log(L) ,但这些都不起作用。

我需要一些帮助。

最佳答案

开始构建。我不得不将图标构造包装在窗口对象的检查中:

// Init custom map icon
if (typeof window !== 'undefined') {
const mapIcon = MapIcon();
markerIcon = new Icon({
iconUrl: mapIcon,
iconSize: [36, 36],
iconAnchor: [18, 18],
shadowSize: [36, 36],
shadowUrl: 'https://unpkg.com/leaflet@1.5.1/dist/images/marker-shadow.png',
shadowAnchor: [10, 18],
popupAnchor: [0, -16],
tooltipAnchor: [13, -4]
});
}

再次将图标传递给标记时:

<Marker position={[node.coords.lat, node.coords.lng]} key={`marker-${index}`} icon={(!!markerIcon) ? markerIcon : null}>

关于Leaflet 自定义图标,react-leaflet,无法构建 Gatsby 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60750863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com