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!
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
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
Nevermind :)
Most helpful comment
I wrap the AGM tag in two container elements
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.
I then use the following functions in the component to capture the idle, centerChange, and resize events
Also have the following variables set
Just make sure you use the Renderer from @angular/core as well as HostListener