使用高徳地图组件ngx-amap ,调用 AmapMouseToolService进行绘制线段时遇到的错误 (ngx-amap地址,AmapMouseToolService地址)
报错代码:
public plugin: Promise<AmapMouseToolWrapper>;
@ViewChild(NgxAmapComponent, {static: true}) private mapComponent: NgxAmapComponent;
this.plugin = this.mapComponent.ready
.pipe(take(1))
.toPromise()
.then((map: Map) => {
console.log(map);
this.map = map;
return this.mouseToolService.of(map);
});
原因:
由于地图渲染时页面还没有初始化完成。上述代码等于是在地图ready的new 了一个AmapMouseToolWrapper。
解决方法:
去掉this.mapComponent.ready .pipe(take(1))的管道和操作符take,
最后代码应该为
public plugin: Promise<AmapMouseToolWrapper>;
@ViewChild(NgxAmapComponent, {static: true}) private mapComponent: NgxAmapComponent;
this.plugin = this.mapComponent.ready
.toPromise()
.then((map: Map) => {
console.log(map);
this.map = map;
return this.mouseToolService.of(map);
});
至于take的意思和用法 参照(这里)
ok,问题解决。