File

projects/ngx-amap/src/components/amap-info-window/amap-info-window.service.ts

Index

Properties
Methods

Constructor

constructor(amaps: AMapService, logger: LoggerService, ngZone: NgZone)
Parameters :
Name Type Optional
amaps AMapService No
logger LoggerService No
ngZone NgZone No

Methods

close
close()

关闭

Returns : void
create
create(options: AMap.InfoWindow.Options)

创建 AMap.InfoWindow

Parameters :
Name Type Optional Description
options AMap.InfoWindow.Options No

选项

Returns : any
destroy
destroy()

销毁

Returns : void
get
get()

获取信息窗体

Returns : any
open
open(position?: AMap.LocationValue)

打开

Parameters :
Name Type Optional
position AMap.LocationValue Yes
Returns : void
openOnMark
openOnMark(marker: Observable)

在覆盖物上打开窗体

Parameters :
Name Type Optional
marker Observable<AMap.Marker | AMap.Text> No
Returns : void

Properties

Private infoWindow
Type : AMap.InfoWindow
Private infoWindow$
Default value : new ReplaySubject<AMap.InfoWindow>(1)
Private map
Type : AMap.Map
import { Injectable, NgZone } from '@angular/core';
import { ReplaySubject, zip, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { AMapService } from '../../shared/amap.service';
import { LoggerService } from '../../shared/logger/logger.service';
import { Getter } from '../../base/interfaces';

const TAG = 'AmapInfoWindow';

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

  constructor(private amaps: AMapService, private logger: LoggerService, private ngZone: NgZone) {}

  /**
   * 获取信息窗体
   */
  get() {
    return this.infoWindow$.asObservable();
  }

  /**
   * 创建 AMap.InfoWindow
   * @param options 选项
   */
  create(options: AMap.InfoWindow.Options) {
    return this.amaps.get().pipe(
      map(m => {
        this.map = m;
        this.infoWindow = this.ngZone.runOutsideAngular(() => new AMap.InfoWindow(options));
        this.logger.d(TAG, 'new InfoWindow created.');
        this.infoWindow$.next(this.infoWindow);
        this.infoWindow$.complete();
        return this.infoWindow;
      }),
    );
  }

  /**
   * 销毁
   */
  destroy() {
    this.get().subscribe(w => {
      w.close();
      this.logger.d(TAG, 'InfoWindow destroyed.');
      this.infoWindow = null;
      this.map = null;
    });
  }

  /**
   * 打开
   */
  open(position?: AMap.LocationValue) {
    this.get().subscribe(w => w.open(this.map, position));
  }

  /**
   * 在覆盖物上打开窗体
   */
  openOnMark(marker: Observable<AMap.Marker | AMap.Text>) {
    zip(marker, this.get()).subscribe(([m, w]) => {
      w.open(this.map, m.getPosition());
    });
  }

  /**
   * 关闭
   */
  close() {
    this.get().subscribe(w => w.close());
  }
}

result-matching ""

    No results matching ""