marker icon isn't showing in Leaflet

后端 未结 4 1529
栀梦
栀梦 2021-01-26 20:46

I \'ve put together a very simple React / Leaflet demo but the marker icon is not showing at all. Full running code is here.

Here\'s what I have in my componentDid

4条回答
  •  攒了一身酷
    2021-01-26 21:20

    Here's what worked for me in the end (I'm on webpack):

    const defaultIcon = new L.icon({
      iconUrl: require('../node_modules/leaflet/dist/images/marker-icon.png'); // your path may vary ...
      iconSize: [8, 8],
      iconAnchor: [2, 2],
      popupAnchor: [0, -2]
    });
    
    generateMarkers().forEach( c=> {
       L.marker(c, {icon: defaultIcon}).addTo(this.map).bindPopup('a marker; yeah').openPopup();
    }
    

提交回复
热议问题