简体   繁体   English

LeafletJS:地图容器已经初始化

[英]LeafletJS: Map container is already initialized

I am using Leaflet in my Ionic 2 app. 我在Ionic 2应用程序中使用Leaflet。 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. 例外:错误:未捕获(承诺):例外:build / pages / map / map.html:12:18错误例外:错误:地图容器已初始化。 ORIGINAL STACKTRACE: Error: Map container is already initialized. 原始堆栈:错误:地图容器已经初始化。

The private variable map is null when coming back to this page. 返回此页面时,私有变量映射为null。 Checking this variable for beeing null has no effect because I think the problem is the new L.Map('mainmap',... 检查此变量是否为空null无效,因为我认为问题是新的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);
  }

}

Probably not the most elegant solution but what worked for me is removing the map when the user leaves the view: 可能不是最优雅的解决方案,但对我有用的是在用户离开视图时删除地图:

ionViewCanLeave(){
    document.getElementById("mainmap").outerHTML = "";
}

Source: https://forum.ionicframework.com/t/map-container-is-already-initialized-error-in-ionic-2/81666 资料来源: https : //forum.ionicframework.com/t/map-container-is-already-initialized-error-in-ionic-2/81666

The if( this.map == null ) condition in your ngOnInit() is always true , since when you instantiate a new MainMapComponent , that new instance will get its own brand new this.map , therefore unassigned (ie undefined ) yet. ngOnInit()if( this.map == null )条件始终为true ,因为当实例化一个新的MainMapComponent ,该新实例将获得自己的全新this.map ,因此尚未分配(即undefined )。

This is totally different from what may have happened to your "mainmap" div container. 这与您的"mainmap" div容器可能发生的情况完全不同。

To be more inline with your problem description, when navigating away from your map, your "mainmap" div container still holds a map . 为了与问题描述更加内联,当离开地图导航时, "mainmap" div容器仍保留一个map When coming back to your page, it looks like your app instantiates a new MainMapComponent instance, which has a new (unassigned yet) this.map , therefore it tries to initialize a new map in "mainmap" div container. 回到页面时,您的应用程序似乎实例化了一个新的MainMapComponent实例,该实例具有一个新的(尚未分配) this.map ,因此它尝试在"mainmap" div容器中初始化一个新地图。 This is what creates the error. 这就是造成错误的原因。

You could try to use this.map.remove() when your MainMapComponent instance is destroyed, so that the "mainmap" div status is in par with the existence (or not) of an instance of MainMapComponent . 您可以在销毁MainMapComponent实例时尝试使用this.map.remove() ,以使"mainmap" div状态与MainMapComponent实例的存在(或不存在) MainMapComponent But that would not solve your problem if for any reason you have more than one instance of MainMapComponent at the same time. 但是,如果由于某种原因您同时拥有多个MainMapComponent实例,那将无法解决您的问题。


Update: 更新:

As for the last mentioned issue, see Initializing leaflet map in angular2 component after DOM object exists 至于最后提到的问题,请参阅在DOM对象存在后在angular2组件中初始化传单地图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM