hosuaby / Leaflet.SmoothMarkerBouncing

Smooth animation of marker bouncing for Leaflet.
BSD 2-Clause "Simplified" License
146 stars 28 forks source link

`marker._icon` is null when marker was set with `marker.setIcon(divIcon) ` #10

Closed raywu closed 6 years ago

raywu commented 6 years ago

Context

Setup

// divIcon
L.divIcon({
  className: 'div-icon',
  iconSize: L.point(20, 20),
  html: `<i class="fas fa-male" style="color: ${ markerColor }"></i>`
});

// set icon on marker instance
marker.setIcon(divIcon)

Problem

// marker._icon
> null

// marker.options.icon
> { options:
      {
        className: "div-icon",
        iconSize: o.Point,
        html: "<i class="fas fa-male" style="color: #ed7ded"></i>"
      },
      _initHooksCalled: true
   }

Question

hosuaby commented 6 years ago

That looks like the bug. I think the reason is that plugin not handles well situation when you change icon after marker creation. Try to solve this problem by defining icon a the same time you create a marker. Look at this example: https://github.com/hosuaby/Leaflet.SmoothMarkerBouncing/blob/fix/icon_size/debug/scripts/divicon.js

raywu commented 6 years ago

@hosuaby Alexei, thanks for the link. I tried your method, but turns out ._icon is still undefined.

// setup
var bounds = [[48.824384, 2.284298], [48.872054, 2.409782]];
var lat = _.random(bounds[0][0], bounds[1][0]);
var lng = _.random(bounds[0][1], bounds[1][1]);

var marker = L.marker([lat, lng], {
            icon: L.divIcon({ className: 'ball-icon' })
        });

// console
marker._icon
> undefined
marker.options.icon
> { options: { className: "ball-icon" }, _initHooksCalled:  true }

I fixed my problem (TypeError) by making sure the marker is added to the relevant layer before calling bounce.

However, ._icon is still undefined and I'm not sure why makeMoveStep and makeResizesStep are not failing, since global icon is still relying on this._icon.

I tried this on Leaflet v1.0.2 (via mabox.js) and v1.3.1. I'll close this issue for now.

omerts commented 6 years ago

Hey, I am also having this issue, but can't addTo before using setIcon, this causes a "style" of undefined error on the following line (855):

// this._icon is undefined
var styles = parseCssText(this._icon.style.cssText);

This is similar to: https://github.com/Leaflet/Leaflet/issues/311