why ngx-leaflet in angular 2 does not render the map after changing state by *ngif

1.4k Views Asked by At

i have a problem with ngx-leaflet component from angular 2. after changing state of *ngif from true to false and again from false to true while map appeared, tiles render not occured. what to i do to avoid this problem?

this is my html code

<button (click)="showMap=!showMap">toggle</button>
<div *ngif="showMap">
  <div [style.height.px]="mapHeight" leaflet
       (leafletClick)="mapOnClick($event)"
       [(leafletCenter)]="mapCenter"
       [leafletOptions]="options"
       (leafletMapReady)="onMapReady($event)"
       [leafletLayers]="layers">
  </div>
</div>

this is my typescript code

options = {
  layers: [
    tileLayer('http://tile.openstreetmap.org/...', 
    { maxZoom: 18, attribution: '...' })
  ],
  zoom: 12,
  center: latLng(35.679966, 51.4)
};

map = null;

onMapReady(map: Map) {
  this.map = map; 
}

mapOnClick(evt) {
}    
1

There are 1 best solutions below

1
On BEST ANSWER

Use getter for options.

  get options(){
    return {
    layers: [
      tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: '...' })
    ],
    zoom: 5,
    center: latLng(46.879966, -121.726909)
    }
  };

Here is the working copy - https://stackblitz.com/edit/angular-w3ugkr