Skip to content

Instantly share code, notes, and snippets.

@VhMuzini
Created June 7, 2018 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VhMuzini/7c98acc213dbb3f10775c719799fd396 to your computer and use it in GitHub Desktop.
Save VhMuzini/7c98acc213dbb3f10775c719799fd396 to your computer and use it in GitHub Desktop.
Implement FITBOUNDS to AGM-MAP
<agm-map #agmmap
[latitude]="lat"
[longitude]="lng"
[maxZoom]="25"
[zoom]="3"
[zoomControl]="true"
[fitBounds]="bounds"
[mapTypeId]="'roadmap'"
[mapTypeControl]="true"
[fullscreenControl]="true"
[scrollwheel]="false"
>
//Necessary import
import { AgmMap, MapsAPILoader } from '@agm/core';
constructor(public mapsAPILoader:MapsAPILoader){
}
search(){
//call this method inside the promisse
this.setBounds();
}
setBounds(){
this.mapsAPILoader.load().then(() =>{
this.bounds = new window['google'].maps.LatLngBounds();
for(let item of this.models){
this.bounds.extend(new window['google'].maps.LatLng(item.model.lat,item.model.lng));
}
});
}
constructor(private mapsAPILoader: MapsAPILoader, private _deviceService: DeviceService) { }
ngOnInit(): void {
let body = this.deviceListCommand.getPostRequest();//get all devices starting out
this._deviceService.getDevices(body)
.subscribe(
(response: IDevice[]) => {
this.markers = response;
this.setBounds();
},
error => console.log(error)
);
}
setBounds(): void {
//wait until google map api loads
this.mapsAPILoader.load().then(() => {
this.latlngBounds = new window['google'].maps.LatLngBounds();
this.markers.forEach((marker) => {
this.latlngBounds.extend(new window['google'].maps.LatLng(marker.LastLatitude, marker.LastLongitude))
})
})
}
References:
https://github.com/SebastianM/angular-google-maps/pull/1389
https://github.com/SebastianM/angular-google-maps/issues/719
https://stackoverflow.com/questions/48865595/is-there-a-way-to-set-the-bounds-and-zoom-level-in-agm-map
@Omar-Salem
Copy link

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment