为agm snazzy-info-window设置信息窗口偏移量

问题描述 投票:1回答:1

我正在使用agm和agm snazzy-info窗口。由于我使用的标记很小,因此信息窗口离标记太远。看看snazzy-info-window文档 - https://github.com/atmist/snazzy-info-window#offset似乎可以让你设置标记的偏移量。好像agm snazzy-info-window隐藏了此选项,请参阅https://angular-maps.com/api-docs/agm-snazzy-info-window/

有没有办法使用agm snazzy-info窗口控制偏移量?

angular-google-maps snazzymaps
1个回答
1
投票

实际上,根据offset propertyangular-google-maps repository没有通过AgmSnazzyInfoWindow组件暴露。绕过这种限制的一个选择是引入一个扩展AgmSnazzyInfoWindow组件的自定义组件,并支持指定offset属性,例如:

import {
  Component,
  AfterViewInit,
  Input
} from "@angular/core";
import { AgmSnazzyInfoWindow } from "@agm/snazzy-info-window";

@Component({
  // tslint:disable-next-line:component-selector
  selector: 'my-info-window',
  template:
    "<div #outerWrapper><div #viewContainer></div></div><ng-content></ng-content>"
})
export class MyInfoWindowComponent extends AgmSnazzyInfoWindow
  implements AfterViewInit {

   /**
   * A custom padding size around the content of the info window.
   */
  @Input() offset: {top: string, left: string};

  ngAfterViewInit() {
    super.ngAfterViewInit();
    this._snazzyInfoWindowInitialized.then(() => {
      this._nativeSnazzyInfoWindow._opts.offset = this.offset;
    });
  }
}

现在可以像这样指定offset:

<agm-map [latitude]="center.lat" [longitude]="center.lng">
  <agm-marker [latitude]="center.lat" [longitude]="center.lng">
    <my-info-window
      [offset]="{
        top: '-60px',
        left: '0px'
      }"
    >
      <ng-template>
        Phoenix
      </ng-template>
    </my-info-window>
  </agm-marker>
</agm-map>

Here is a demo

© www.soinside.com 2019 - 2024. All rights reserved.