Home > OS >  Leaflet map is undefined and initialized at same time
Leaflet map is undefined and initialized at same time

Time:11-25

I'm developing an Ionic app, one of the app's page is a place detail with Leaflet map section.

First time when i enter on a place detail page the map works fine, but when go back and enter on another place detail page an error appears:

Error: "Map container is already initialized."

I have added part of code to turn off and remove the map but never goes inside because always is undefined but when I try to create it it tells me that it is initialized.

component.st:

ionViewWillEnter() {
  if (this.placeMapLeaflet) {
    this.placeMapLeaflet.off();
    this.placeMapLeaflet.remove();
  }
this.placeMapLeaflet = L.map("place_map_leaflet").setView([position.lat, position.lng], this.zoom);
}

The problem is that the second time ALWAYS is undefined but it's initialized.

Anybody could help me please ?

CodePudding user response:

SOLVED with lifecycles:

...
ionViewDidEnter() {
  this.placeMapLeaflet = L.map("place_map_leaflet").setView([position.lat, position.lng], this.zoom);
}

ionViewDidLeave() {
  this.placeMapLeaflet.off();
  this.placeMapLeaflet.remove();
}
...
  • Related