Here map javascript api for Angular App,打开here map页面发生内存泄漏
Here map javascript api for Angular App, memory leak happen for open the here map page
每次打开这里地图页面时:
this.router.navigate(['/car-section/here-map'], {replaceUrl: false});
html此处地图页面代码:
<div #map class="map-container">
此处地图组件:
export class HereMapComponent implements OnInit {
private platform: any;
private mapHere:any;
........
@ViewChild("map", {static: true}) public mapElement: ElementRef;
constructor() {
this.platform = new H.service.Platform({
'apikey': API_Key
});
}
......
public ngAfterViewInit() {
setTimeout(() => {
let defaultLayers = this.platform.createDefaultLayers();
this.mapHere = new H.Map(
this.mapElement.nativeElement,
defaultLayers.vector.normal.map, //3.1
// defaultLayers.normal.map, //3.0
{
zoom: 16,
center: this.center,
pixelRatio: window.devicePixelRatio || 1
}
);
}, 300);
}
获取内存泄漏,每次获取两个内存堆泄漏:
enter image description here
这并不奇怪,因为 Angular 组件生命周期在您离开视图时被销毁,并在您导航到视图时(重新)创建,而 HERE 地图对象驻留在在全局 H 对象上。
我认为您的 Angular 组件应该与 OnDestroy 交互,因此实现了 ngOnDestroy 方法。在该方法中,应该 dispose 已经 "imposed" 到地图的对象和处理程序。
通常,我们需要处理事件处理程序,然后是映射对象,然后是映射本身。
例如,参见 H.Map API Reference.
上的 dispose 方法
每次打开这里地图页面时:
this.router.navigate(['/car-section/here-map'], {replaceUrl: false});
html此处地图页面代码:
<div #map class="map-container">
此处地图组件:
export class HereMapComponent implements OnInit {
private platform: any;
private mapHere:any;
........
@ViewChild("map", {static: true}) public mapElement: ElementRef;
constructor() {
this.platform = new H.service.Platform({
'apikey': API_Key
});
}
......
public ngAfterViewInit() {
setTimeout(() => {
let defaultLayers = this.platform.createDefaultLayers();
this.mapHere = new H.Map(
this.mapElement.nativeElement,
defaultLayers.vector.normal.map, //3.1
// defaultLayers.normal.map, //3.0
{
zoom: 16,
center: this.center,
pixelRatio: window.devicePixelRatio || 1
}
);
}, 300);
}
获取内存泄漏,每次获取两个内存堆泄漏:
enter image description here
这并不奇怪,因为 Angular 组件生命周期在您离开视图时被销毁,并在您导航到视图时(重新)创建,而 HERE 地图对象驻留在在全局 H 对象上。
我认为您的 Angular 组件应该与 OnDestroy 交互,因此实现了 ngOnDestroy 方法。在该方法中,应该 dispose 已经 "imposed" 到地图的对象和处理程序。 通常,我们需要处理事件处理程序,然后是映射对象,然后是映射本身。 例如,参见 H.Map API Reference.
上的 dispose 方法