Search code examples
angular-google-mapssnazzymaps

Setting info-window offset for agm snazzy-info-window


I'm using agm and agm snazzy-info window. Since the marker I'm using is small, the info window is too far from the marker. Looking at snazzy-info-window docs - https://github.com/atmist/snazzy-info-window#offset seems like they let you set the offset from the marker. It seems like agm snazzy-info-window hide this option, see https://angular-maps.com/api-docs/agm-snazzy-info-window/.

Is there any way to control the offset using agm snazzy-info window?


Solution

  • Indeed, offset property according to angular-google-maps repository is not exposed via AgmSnazzyInfoWindow component. One option to circumvent this limitation would be to introduce a custom component which extends AgmSnazzyInfoWindow component and supports to specify offset property, for example:

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

    Now offset could be specified like this:

    <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