简体   繁体   English

为什么我的自定义 React-leaflet 标记没有显示在我的地图上?

[英]Why is my custom React-leaflet marker not displaying on my map?

I am trying to make a custom marker icon for my leaflet component in react.我正在尝试为我的传单组件制作自定义标记图标。 I am defining an icon class and attempting make the iconURL the location of the PNG I use.我正在定义一个图标类并尝试使 iconURL 成为我使用的 PNG 的位置。 The issue that I am running into is that the iconURL is only working for 'https://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|abcdef&chf=a,s,ee00FFFF'.我遇到的问题是 iconURL 仅适用于“https://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|abcdef&chf=a,s,ee00FFFF”。

I also tried using the directory of a PNG in my project, as well as a URL of an icon provided by google, for example 'https://fonts.google.com/icons?selected=Material%20Icons%20Outlined%3Acable%3A'.我还尝试在我的项目中使用 PNG 的目录,以及谷歌提供的图标的 URL,例如 'https://fonts.google.com/icons?selected=Material%20Icons%20Outlined%3Acable% 3A'。 These both however do not display the image correctly.然而,这两者都不能正确显示图像。 Is it an issue with the specific PNGs that I am using?这是我使用的特定 PNG 的问题吗? I tried overriding the default URL icon with L.Icon.Default.prototype.options.iconUrl however it just produced the same results.我尝试用 L.Icon.Default.prototype.options.iconUrl 覆盖默认的 URL 图标,但它只是产生了相同的结果。 Below will also be an image of what the icon looks like when attempting to use the icons that do not work currently.下面还将是尝试使用当前不起作用的图标时图标外观的图像。

(EDIT: Including an image of my directory in case it helps) (编辑:包括我的目录的图像,以防万一) 在此处输入图片说明

图标显示不正确

 function OutageIndicator({ outage }) { const LeafIcon = L.Icon.extend({ options: { iconSize: [38, 95], shadowSize: [50, 64], iconAnchor: [22, 94], shadowAnchor: [4, 62], popupAnchor: [-3, -76] } }); L.Icon.Default.prototype.options.iconUrl = 'icons/marker.png'; const streamingIcon = new LeafIcon({ iconUrl:"icons/marker.png" }); return !coords ? ( "Loading" ) : ( <Marker position={[coords.lat, coords.lng]} icon={streamingIcon}> <Popup className={outage.service_type}> {outage.service_type}: {outage.service_name} </Popup> </Marker> ); } function OutageMap() { //a bunch of other lines of code return ( <> <button onClick={setReportIsOpenTrue}>Report Outage</button> <MapContainer center={[44, -85]} zoom={7} scrollWheelZoom={true}> <TileLayer attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> {allOutages.map((mock) => ( <OutageIndicator outage={mock} /> ))} </MapContainer> </> ); } export default OutageMap;

Since you are using React, there is probably a webpack build tool configured under the hood, with a file or url-loader.由于您使用的是 React,因此可能在后台配置了一个 webpack 构建工具,带有文件或 url-loader。

In that case, you have to import or require your icon file, so that webpack knows that it must include it in your bundle:在这种情况下,您必须导入或要求您的图标文件,以便 webpack 知道它必须包含在您的包中:

    const streamingIcon = new LeafIcon({
      iconUrl: require("./icons/marker.png")
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM