【发布时间】:2018-04-11 22:39:41
【问题描述】:
我在 Ionic 2 应用程序中使用 Leaflet。第一次运行应用程序时。一切都很好。但是,如果我转到另一个页面并返回地图,则会出现以下异常:
异常:错误:未捕获(承诺中):异常:构建/页面/map/map.html:12:18 中的错误 原始异常:错误:地图容器已初始化。 原始堆栈跟踪: 错误:地图容器已经初始化。
返回此页面时,私有变量映射为空。检查此变量是否为空无效,因为我认为问题出在 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);
}
}
【问题讨论】:
-
与你的组件关联的模板代码是什么?你的
mainmap在哪里?在您的组件模板中?谢谢!
标签: angularjs angular ionic-framework leaflet ionic2