I'm currently trying to create a leaflet map inside an angular material2 tab-group
as below
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { MaterialModule } from '@angular/material';
import { Component, OnInit, OnDestroy } from '@angular/core';
import 'leaflet';
@Component({
selector: 'minimap',
template: `<div #minimap [id]="id" class=leaflet-map></div>`
})
export class MiniMap implements OnInit, OnDestroy {
map: L.Map = null;
id: string;
constructor() {
this.id = "map" + Date.now();
}
ngOnInit() {
this.map = L.map(this.id).setView([54.5, -115.0], 13);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution:
'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(this.map);
}
ngOnDestroy() { }
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<md-tab-group>
<md-tab label="tab 1">
tab 1 content
</md-tab>
<md-tab label="tab 2">
tab 2 content
</md-tab>
<md-tab label="map tab">
<minimap></minimap>
</md-tab>
</md-tab-group>
</div>
`,
})
export class App {
name:string;
constructor() {
this.name = 'Angular2'
}
}
https://plnkr.co/edit/dLwhv3XoMWMYFkqztkuR?p=preview
Unfortunately when it tries to create the map in the component it throws an error saying that the map container doesn't exist yet. my interpretation is that the code is currently running before the DOM object is created.
What is the correct way of creating the map such that the code is called when the DOM object exists??