简体   繁体   English

谷歌地图仅在第一次加载,第二次加载为灰色

[英]Google maps only load the first time, second time it is gray

I have created an Ionic 2 / Angular 2 App. 我创建了一个Ionic 2 / Angular 2 App。 I have integrated Google Maps and it works fine. 我已经集成了谷歌地图,它工作正常。 However the second time the Google Maps appears completely gray. 然而,谷歌地图第二次出现完全灰色。 I have searched the internet and tried different solutions to solve it. 我搜索了互联网并尝试了不同的解决方案来解决它。 However, none of the solutions below work: - google.maps.event.trigger(this.map, 'resize'); 但是,以下解决方案都不起作用: - google.maps.event.trigger(this.map,'resize'); - google.maps.event.clearInstanceListeners / this.map.detach(); - google.maps.event.clearInstanceListeners / this.map.detach(); / this.marker.setMap(null); / this.marker.setMap(null);

When I resize the view in chrome then the maps which appeared in gray is suddenly shown. 当我在chrome中调整视图大小时,突然显示以灰色显示的地图。 So how can I trigger this resize manually. 那么如何手动触发此调整大小。 I hope you can help. 我希望你能提供帮助。

My code can be found here https://github.com/marinusgeuze/metjekindnaarbuiten . 我的代码可以在这里找到https://github.com/marinusgeuze/metjekindnaarbuiten

Best regards, Marinus Geuze 最诚挚的问候,Marinus Geuze

=========================================== ===========================================

Issue is solved by adding next line of code: setTimeout(() => google.maps.event.trigger(this.map, 'resize'), 600); 通过添加下一行代码来解决问题:setTimeout(()=> google.maps.event.trigger(this.map,'resize'),600);

I didn't have exactly the same issue, but something similar happen when using LeafletJS in my Ionic 2 app, and a call to their invalidateSize function on a small timeout fixed the issue for my app. 我没有完全相同的问题,但在我的Ionic 2应用程序中使用LeafletJS时发生了类似的事情,并且在一个小超时上调用了invalidateSize函数修复了我的应用程序的问题。 Here's an example: 这是一个例子:

ionViewLoaded() {
    console.log('ObservationDetails.ionViewLoaded');

    // Leaflet setup calls here
    ...

    // invalidateSize must be called because the page hasn't been drawn, only loaded,
    // and the map div will get resized causing the "gray block/unloaded map tile"
    // problem. Invalidating the map will fix that, but the call needs a slight 
    // delay before firing...
    setTimeout(() => this.map.invalidateSize(), 600);
}

Solution for map, Use Below code of line to resolve the issue : 地图解决方案,使用下面的代码解决问题:

// you must use "map" variable name, or you can use your defined variable name
// Example : var map = (new google.maps.Map(...));

setTimeout(function() { google.maps.event.trigger(map, 'resize') }, 600);

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

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