I am using Leaflet in my Ionic 2 app. When running the app first time. Everyhting is fine. But if I go to another page and back to the map I get the following exception:
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/map/map.html:12:18 ORIGINAL EXCEPTION: Error: Map container is already initialized. ORIGINAL STACKTRACE: Error: Map container is already initialized.
The private variable map is null when coming back to this page. Checking this variable for beeing null has no effect because I think the problem is the new L.Map('mainmap',...
export class MainMapComponent {
private map;
constructor(
private mapService: MapService,
private geoCodingService: GeocodingService) { }
ngOnInit() {
console.log(this.map);
if( this.map == null ) this.initMap();
}
initMap() {
console.log('init');
if(this.map) this.map.remove();
this.map = new L.Map('mainmap', {
zoomControl: false,
center: new L.LatLng(40.731253, -73.996139),
zoom: 12,
minZoom: 4,
maxZoom: 19,
layers: [this.mapService.baseMaps.OpenStreetMap],
attributionControl: false
});
console.log(this.map);
}
}