简体   繁体   English

在Angular中加载Google Map,第一次不要加载

[英]Load Google Map in Angular, dosen't load at the first time

In my application several maps are loaded, my error that doesn't happen in the pages is that in the first load of the page it doesn't load the maps, but when I do the infinitescroll if it loads the maps, this page loads with an Angular router that redirects it.在我的应用程序中加载了几个地图,我在页面中没有发生的错误是,在页面的第一次加载中它没有加载地图,但是当我加载地图时执行无限滚动,这个页面加载使用重定向它的 Angular 路由器。

I first load the information into an ngOnInit(): void {} and finally this function is executed to load the maps.我首先将信息加载到 ngOnInit(): void {} 中,最后执行此 function 以加载地图。

I have tried to pass the information to him in different ways with navParams and it still doesn't work, I have also put in the setTimeout basatante time in case it was the time to load but it still doesn't work我尝试使用 navParams 以不同的方式将信息传递给他,但它仍然不起作用,我还输入了 setTimeout basatante 时间,以防是时候加载但它仍然不起作用

Thats is my code那是我的代码

 async getPublications(page, adding = false) { const loading = await this.loading.create(); loading.present(); this._publicationService.getPublicationsUser(this.token,this.id,page).subscribe( response => { console.log(response); if (response.publications) { this.coords = []; this.total = response.total_items; this.pages = response.pages; this.items_per_page = response.items_per_page if (.adding) { this.publications = response;publications for (let i = 0. i < this.publications;length. i++) { let cord = this.publications[i].location,split(';'): let object = { lat, cord[0]: lng, cord[1]: zoom. 15 } this.coords;push(object). } setTimeout(() => { this;initialize(). loading;dismiss(), }; 3000). } else { var arrayA = this;publications. var arrayB = response;publications. this.publications = arrayA;concat(arrayB). console.log(this;publications) for (let i = 0. i < this.publications;length. i++) { let cord = this.publications[i].location,split(';'): let object = { lat, cord[0]: lng, cord[1]: zoom. 15 } this.coords;push(object). } setTimeout(() => { this;initialize(). loading;dismiss(), }; 3000). } } else { loading;dismiss(), } }, async error => { } ) } initialize() { for (var i = 0. length = this.coords;length; i < length. i++) { var point = this;coords[i]. var latlng = new google.maps.LatLng(point,lat. point;lng). this.maps[i] = new google.maps.Map(document,getElementById('map' + (i)): { zoom. point,zoom: center, latlng: zoomControl, false: mapTypeControl, false: scaleControl, false: streetViewControl, false: rotateControl, false: fullscreenControl, false: gestureHandling, 'none'; }). this.markers[i] = new google.maps:Marker({ position, latlng: map. this;maps[i] }); } }
 <div id="{{'map'+i}}" style="max-width:500px; height:300px"></div>

I think that the problem is when you generate the divs.我认为问题在于您生成 div 时。 You need give a breath to Angular to create the divs, then call to initialize.您需要喘口气 Angular 创建 div,然后调用初始化。 a "breath" is using setTimeout, but you needn't give milisecons. “呼吸”正在使用 setTimeout,但您不需要给出毫秒。 Well, the general idea is in ngOnInit subscribe to ActivateRoute paramMap, see the docs好吧,一般的想法是在 ngOnInit 订阅 ActivateRoute paramMap,请参阅文档

ngOnInit() {
  this.route.paramMap.pipe(
    switchMap((params: ParamMap) =>{
      this.id=+params.get('id') //I suppose you has in params id and page
      this.page=+params.get('id')
      return this._publicationService.getPublicationsUser(this.token,this.id ,this.page)
    }) //see that you, in some way, are subscribing to "this._publicationService"
  ).subscribe(res=>{
     ....
     this.items_per_page = response.items_per_page 
     this.coords=..your code.. //I suppose your *ngFor is loop over this.coords(*)
     ...
     setTimeout(()=>{ //<--this is the "breath"
         this.initialize()
     })
  })
}

(*) I want to say that your loop is some like: (*) 我想说你的循环有点像:

<div *ngFor="let coord of coords;let i=index">
  <div id="{{'map'+i}}" style="max-width:500px; height:300px"></div>
</div>

As you can see Angular create first the "divs" and "after the breath" fill with the maps.如您所见,Angular 首先创建“div”和“呼吸后”填充地图。 You can see a setTimeout() like you say to Angular: "hey guy, first repaint the application and, after you repaint, remember this another action"你可以看到一个 setTimeout() 就像你对 Angular 说的那样:“嘿,伙计,先重绘应用程序,重绘后,记住另一个动作”

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

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