File

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

Index

Methods

Constructor

constructor(pixels: PixelService, sizes: SizeService)
Parameters :
Name Type Optional
pixels PixelService No
sizes SizeService No

Methods

create
create(options: string | AMap.Icon | IIcon)
Parameters :
Name Type Optional
options string | AMap.Icon | IIcon No
Returns : string | AMap.Icon | null
import { Injectable } from '@angular/core';
import { PixelService } from './pixel.service';
import { SizeService } from './size.service';
import { IIcon } from '../interfaces';

@Injectable({
  providedIn: 'root',
})
export class IconService {
  constructor(private pixels: PixelService, private sizes: SizeService) {}

  create(options: string | AMap.Icon | IIcon): string | AMap.Icon | null {
    if (!options) {
      return null;
    }

    if (typeof options === 'string') {
      return options;
    }

    if (options instanceof AMap.Icon) {
      return options;
    }

    const iconOption: AMap.Icon.Options = {};
    const { size, image, imageOffset, imageSize } = options;
    if (size !== undefined) {
      iconOption.size = this.sizes.create(size);
    }
    if (image !== undefined) {
      iconOption.image = image;
    }
    if (imageOffset !== undefined) {
      iconOption.imageOffset = this.pixels.create(imageOffset);
    }
    if (imageSize !== undefined) {
      iconOption.imageSize = this.sizes.create(imageSize);
    }
    return new AMap.Icon(iconOption);
  }
}

result-matching ""

    No results matching ""