bozdoz / wp-plugin-leaflet-map

Add leaflet maps to Wordpress with shortcodes
https://wordpress.org/plugins/leaflet-map/
GNU General Public License v2.0
140 stars 71 forks source link

Re-initialise map #152

Closed merijnponzo closed 1 year ago

merijnponzo commented 2 years ago

I was looking for a way to re-initalise a map, for example: the map is already rendered with the shortcode in a popup, and needs to be shown when the popup is active.

map.invalidateSize() with a timeout does the trick, is this the way to go?

export const Maps = () => {
  return {
    active: true,
    init() {},
    setActive() {
      this.active = !this.active;
      if (window.WPLeafletMapPlugin) {
        // window.WPLeafletMapPlugin.init();
        const mapsFound = window.WPLeafletMapPlugin.maps;
        for (var i = 0, len = mapsFound.length; i < len; i++) {
          var map = mapsFound[i];
          setTimeout(function () {
            map.invalidateSize();
          }, 600);
        }
      }
    },
  };
};

// export all blocks
export default { Maps };
bozdoz commented 1 year ago

Might be. Sorry. This is typically a difficult problem to solve, and would need a custom solution for each situation