Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
raywu opened this issue Mar 16, 2018 · 3 comments
Closed

Comments

@raywu
Copy link

raywu commented Mar 16, 2018

Context

  • v1.1.4
  • TypeError: Cannot read property 'style' of null
  • this happens on line 626 makeMoveStep and 668 makeResizesStep when calling icon.style.cssText
  • related issue, but not the same

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

  • I notice that my markers don't have ._icon
// 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

  • Is there a preferred way to use divIcon with this plugin? What am I doing wrong with marker and divIcon?
@hosuaby
Copy link
Owner

hosuaby commented Mar 16, 2018

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
Copy link
Author

raywu commented Mar 16, 2018

@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.

@raywu raywu closed this as completed Mar 16, 2018
@omerts
Copy link

omerts commented Jul 3, 2018

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:
Leaflet/Leaflet#311

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants