dev*_*evC 4 google-maps angular-google-maps
要设置地图缩放级别包括所有的位置标记,我已经尝试了两种选择,如建议这个职位。
这是我所做的:
export class LocationSelectionComponent implements OnInit, AfterViewInit {
@ViewChild('AgmMap') agmMap: AgmMap;
ngAfterViewInit() {
console.log(this.agmMap);
this.agmMap.mapReady.subscribe(map => {
const bounds: LatLngBounds = new window['google'].maps.LatLngBounds();
for (const mm of this.mapMarkers) {
if (mm.latitude !== this.currentLocationLatitude && mm.longitude !== this.currentLocationLongitude) {
bounds.extend(new window['google'].maps.LatLng(mm.latitude, mm.longitude));
}
}
map.fitBounds(bounds);
});
}
}
Run Code Online (Sandbox Code Playgroud)
请注意, this.mapMarkers 是一个数组,其中包含地图标记的坐标。这些填充在ngOnInit().
正如上面帖子的评论中提到的,我还尝试了以下方法:
onMapReady(map: AgmMap) {
const bounds: LatLngBounds = new window['google'].maps.LatLngBounds();
for (const mm of this.mapMarkers) {
if (mm.latitude !== this.currentLocationLatitude && mm.longitude !== this.currentLocationLongitude) {
bounds.extend(new window['google'].maps.LatLng(mm.latitude, mm.longitude));
}
}
// @ts-ignore
map.fitBounds(bounds);
}
Run Code Online (Sandbox Code Playgroud)
然后在 HTML 中:
<agm-map #AgmMap [latitude]="latitude" [longitude]="longitude"
[fullscreenControl]="true" [mapTypeControl]="true" (mapReady)="onMapReady($event)">
<agm-marker *ngFor="let m of mapMarkers; let i = index"
[latitude]="m.latitude"
[longitude]="m.longitude"
[title]="m.title"
[iconUrl]="m.iconUrl"
[animation]="m.animation"
(markerClick)="clickedMarker(m.label)">
</agm-marker>
</agm-map>
Run Code Online (Sandbox Code Playgroud)
但在这两种情况下,我都没有像预期的那样缩小地图。原因是,当我调试代码时,这两种情况下的 mapMarkers 数组都是空的。我该如何解决?
小智 11
添加[fitBounds]="true"到<agm-map>
添加[agmFitBounds]="true"到<agm-marker>
删除[usePanning]="true"从<agm-map>
为了更多的可用性添加集群: 安装这个模块并按照说明进行操作
| 归档时间: |
|
| 查看次数: |
5360 次 |
| 最近记录: |