如何在传单地图中设置标记图标?

les*_* n. 0 javascript leaflet reactjs

我有我的组件 MAP 并且使用传单(不是反应传单)。我想设置一个标记。

这是我的组件的代码。

import React from 'react';
import L from 'leaflet';
import '../../../../node_modules/leaflet/dist/leaflet.css';
import './map.scss';



export default class Map extends React.Component {


  componentDidMount() {

    this.map = L.map('map', {
      center: [48.8762, 2.357909999999947],
      zoom: 14,
      zoomControl: true,
      layers: [
        L.tileLayer('https://{s}.tile.openstreetmap.de/tiles/osmde/{z}/{x}/{y}.png',{
          detectRetina: true,
          maxZoom: 20,
          maxNativeZoom: 17,
        }),
      ]
    });

    L.marker([48.8762, 2.357909999999947],
      {
        draggable: true,        // Make the icon dragable
        title: 'Hover Text',     // Add a title
        opacity: 0.5}            // Adjust the opacity
    )
      .addTo(this.map)
      .bindPopup("<b>Paris</b><br>Gare de l'Est")
      .openPopup();

    L.circle([48.8762, 2.357909999999947], {
      color: 'red',
      fillColor: '#f03',
      fillOpacity: 0.5,
      radius: 500
    }).addTo(this.map);

  };


  render() {
    return <div className="map" id="map" />
  }
}
Run Code Online (Sandbox Code Playgroud)

我添加了文档中描述的标记,但地图中的结果不是默认图标,它只是一个带有细边框的正方形。我检查了传单中的 css 文件。该图标位于图像文件夹中,但不在可用地图中。

我正在寻找这个图标(标记) 在此输入图像描述

这就是我得到的 在此输入图像描述

有人可以帮忙看看我的代码有什么问题吗?

kbo*_*oul 6

这是一个众所周知的 webpack css 捆绑问题。将markerIcon定义为

const markerIcon = L.icon({
  iconSize: [25, 41],
  iconAnchor: [10, 41],
  popupAnchor: [2, -40],
  // specify the path here
  iconUrl: "https://unpkg.com/leaflet@1.5.1/dist/images/marker-icon.png",
  shadowUrl: "https://unpkg.com/leaflet@1.5.1/dist/images/marker-shadow.png"
});
Run Code Online (Sandbox Code Playgroud)

并将其分配给 icon 属性,L.marker如下所示:

 L.marker(
      [48.8762, 2.357909999999947],
      {
        draggable: true, // Make the icon dragable
        title: "Hover Text", // Add a title
        opacity: 0.5,
        icon: markerIcon // here assign the markerIcon var
      } // Adjust the opacity
    )
      .addTo(this.map)
      .bindPopup("<b>Paris</b><br>Gare de l'Est")
      .openPopup();
Run Code Online (Sandbox Code Playgroud)

演示