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

时间:2019-07-18 09:49:20

标签: reactjs leaflet

我有我的组件MAP,并且我使用传单(而不是react-leaflet)。 我要设置一个标记。

这是我组件的代码。

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" />
  }
}

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

我要寻找的这个图标(标记) enter image description here

这就是我得到的 enter image description here

任何人都可以帮忙或者看看我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:2)

这是webpack的CSS捆绑包问题。将markerIcon定义为

public File generateDefaultJavaFile(CustomPlugin plugin) throws IOException{
    File main = plugin.getMain();

    BufferedWriter writer = new BufferedWriter(new FileWriter(main));
    writer.write("");
}

像这样将其分配给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" }); 的icon属性:

L.marker

Demo

答案 1 :(得分:0)

根据您的问题,我可以解释为您无法正确导入CSS。

您可以通过编写导入传单模块的css

import 'leaflet/dist/leaflet.css';

有关详细说明,您可以阅读

Do I have to import leaflet's CSS file?

Example of how to load static CSS files from node_modules using webpack?

相关问题