Created
June 7, 2018 17:48
-
-
Save VhMuzini/7c98acc213dbb3f10775c719799fd396 to your computer and use it in GitHub Desktop.
Implement FITBOUNDS to AGM-MAP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<agm-map #agmmap | |
[latitude]="lat" | |
[longitude]="lng" | |
[maxZoom]="25" | |
[zoom]="3" | |
[zoomControl]="true" | |
[fitBounds]="bounds" | |
[mapTypeId]="'roadmap'" | |
[mapTypeControl]="true" | |
[fullscreenControl]="true" | |
[scrollwheel]="false" | |
> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
}) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you!