File

projects/ngx-amap/src/shared/amap.service.ts

Index

Properties
Methods

Constructor

constructor(loader: AMapLoaderService, logger: LoggerService, ngZone: NgZone)
Parameters :
Name Type Optional
loader AMapLoaderService No
logger LoggerService No
ngZone NgZone No

Methods

create
create(container: HTMLElement, options: AMap.Map.Options)

创建地图对象 AMap.Map

Parameters :
Name Type Optional Description
container HTMLElement No

地图容器的DOM元素

options AMap.Map.Options No

选项

Returns : any
destroy
destroy()

销毁地图对象

Returns : void
get
get()

获取地图对象 AMap.Map

Returns : any

Properties

Private map
Type : AMap.Map
Private map$
Default value : new ReplaySubject<AMap.Map>()
import { Injectable, NgZone } from '@angular/core';
import { ReplaySubject } from 'rxjs';
import { map } from 'rxjs/operators';
import { AMapLoaderService } from './amap-loader.service';
import { LoggerService } from './logger/logger.service';
import { Getter } from '../base/interfaces';

const TAG = 'AMap';

@Injectable()
export class AMapService implements Getter<AMap.Map> {
  private map: AMap.Map;
  private map$ = new ReplaySubject<AMap.Map>();

  constructor(
    private loader: AMapLoaderService,
    private logger: LoggerService,
    private ngZone: NgZone,
  ) {}

  /**
   * 获取地图对象 AMap.Map
   */
  get() {
    return this.map$.asObservable();
  }

  /**
   * 创建地图对象 AMap.Map
   * @param container 地图容器的DOM元素
   * @param options 选项
   */
  create(container: HTMLElement, options: AMap.Map.Options) {
    return this.loader.load().pipe(
      map(() => {
        this.map = this.ngZone.runOutsideAngular(() => new AMap.Map(container, options));
        this.logger.d(TAG, 'new map created.');
        this.map$.next(this.map);
        this.map$.complete();
        return this.map;
      }),
    );
  }

  /**
   * 销毁地图对象
   */
  destroy() {
    this.get().subscribe(m => {
      m.destroy();
      this.logger.d(TAG, 'map destroyed.');
      this.map = null;
    });
  }
}

result-matching ""

    No results matching ""