angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

How to fit the height of map to screen size

Open dineshkumar20 opened this issue 8 years ago • 3 comments

I want to fit the height of map to Screen size. I tried height:100%. But it doesn't work. Can you please tell me how to do it? Thanks!

dineshkumar20 avatar Feb 04 '18 18:02 dineshkumar20

I wrap the AGM tag in two container elements

<div class="outer-wrapper">
  <div class="map-wrapper" id="wrapper" #wrapper>
    <agm-map
      [latitude]="latitude"
      [longitude]="longitude"
      [scrollwheel]="true"
      [zoom]="zoom"
      (idle)="idle()"
      (centerChange)="centerChange($event)"
      id="AgmMap"
      #AgmMap>
      <agm-marker 
         [latitude]="latitude"
         [longitude]="longitude"
         [markerClickable]="true"></agm-marker>
    </agm-map>
  </div>
</div>

I then set the outer container to 100%, the inner container to some arbitrary height (it doesn't matter), and the AGM element to 100% through CSS like this.

.outer-wrapper {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;

  .map-wrapper {
    height: 400px;

    agm-map {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
  }
}

I then use the following functions in the component to capture the idle, centerChange, and resize events

@HostListener('window:resize')
  onWindowResize() {
    this.onResize();
  }

  ngAfterViewInit() {
    this.renderer.setElementStyle(
      this.wrapper.nativeElement, 'height',
      (window.innerHeight) + 'px'
    );
  }

  onResize() {
    // resize the container for the google map
    this.renderer.setElementStyle(
      this.wrapper.nativeElement, 'height',
      (window.innerHeight ) + 'px'
    );

    // recenters the map to the resized area.
    this.agmMap.triggerResize().then(() =>  
       this.agmMap._mapsWrapper.setCenter({lat: this.centerLat, lng: this.centerLng}));
  }

  // idle fires after paning or zooming is done. 
  // This is where we want to capture the center of the map.
  // This way if the user resizes, the center is preserved.
  idle() {
    this.centerLat = this.changeLat;
    this.centerLng = this.changeLng;
  }

// this event fires whenever any event changes the center. Panning, zooming, or resizing.
  centerChange(event: any) {
    if (event) {
      this.changeLat = event.lat;
      this.changeLng = event.lng;
    }
  }

Also have the following variables set

@ViewChild('AgmMap') agmMap: any;
  @ViewChild('wrapper') wrapper: ElementRef;

public latitude: number;
  public longitude: number;
  private centerLat: number;
  private centerLng: number;
  public zoom: number;

  private changeLat: number;
  private changeLng: number;

Just make sure you use the Renderer from @angular/core as well as HostListener

timcblank avatar Feb 07 '18 20:02 timcblank

Hi,

Good day.

Works for a resize, quick question, how would I set this at first load? When the page loads in the web browser, to have it resize to fit the current screen? Thanks.

Regards. JJ

jarrettj avatar Sep 14 '18 07:09 jarrettj

Nevermind :)

jarrettj avatar Sep 14 '18 07:09 jarrettj