【问题标题】:Setting info-window offset for agm snazzy-info-window为 agm snazzy-info-window 设置信息窗口偏移量
【发布时间】:2019-02-17 06:15:23
【问题描述】:

我正在使用 agm 和 agm snazzy-info 窗口。由于我使用的标记很小,信息窗口离标记太远了。 查看时髦的信息窗口文档- 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】:

    确实,offset property 根据angular-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;
        });
      }
    }
    

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

    <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

    【讨论】:

    • 这里的解决方案很好。我需要使用@agm/snazzy-info-window 中不包含的基础属性edgeOffset。这运作良好。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多