4

我正在使用 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 窗口控制偏移量?

4

1 回答 1

4

实际上,根据angular-google-maps 存储库的offset属性不会通过组件公开。规避此限制的一种选择是引入一个自定义组件,该组件扩展组件并支持指定属性,例如:AgmSnazzyInfoWindow AgmSnazzyInfoWindowoffset

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;
    });
  }
}

现在可以像这样指定偏移量:

<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>

这是一个演示

于 2019-02-16T22:10:02.453 回答