semantic_c0d3r
semantic_c0d3r

Reputation: 521

Leaflet divIcon not displaying on map in angular

I am trying to add a divicon marker in angular leaflet.

component.css:

.leaflet-div-icon2
{
    background: #e5001a;
    border:5px solid rgba(255,255,255,0.5);
    color:blue;
    font-weight:bold;
    text-align:center;
    border-radius:50%;
    line-height:30px;
}

component.ts :

var map = L.map('map', {
        attributionControl: false,
        crs: L.CRS.Simple
});

var bounds = [[0,0], [1000,1000]];
var image = L.imageOverlay('cust_image.png', bounds).addTo(map);
var customMarkerIcon = L.divIcon({className: 'leaflet-div-icon2'});

var m3 = L.latLng([ 348,278.2]);
L.marker(m3, {icon: customMarkerIcon }).addTo(map);

I am not getting any error in console but unable to view the marker on the map.

It works on basic html page w/ css and js but not in angular.

Am I missing something here?

Upvotes: 4

Views: 2727

Answers (1)

kboul
kboul

Reputation: 14600

I am not sure why it is not working for you but you need to place the code inside ngOnInit lifecycle hook and L.divIcon class selector style inside global styles.css. Here is a similar example with what you are trying to achieve:

app.ts:

ngOnInit() {
    const map = L.map("map", {
      crs: L.CRS.Simple
    });

    const bounds = [[0, 0], [1000, 1000]];

    map.fitBounds(bounds);

    const image = L.imageOverlay(
      "https://leafletjs.com/examples/crs-simple/uqm_map_full.png",
      bounds
    ).addTo(map);
    const customMarkerIcon = L.divIcon({
      className: "leaflet-div-icon2"
    });

    const m3 = L.latLng([348, 278.2]);
    L.marker(m3, {
      icon: customMarkerIcon
    }).addTo(map);
}

styles.css:

.leaflet-div-icon2 {
  background: #e5001a;
  border: 5px solid rgba(255, 255, 255, 0.5);
  color: blue;
  font-weight: bold;
  text-align: center;
  border-radius: 50%;
  line-height: 30px;
}

Demo

Upvotes: 7

Related Questions