我正在使用 Angular 6 和 ngx-leaflet。我正在加载数据后端,并且在地图上加载数据后尝试缩放到区域(在本例中为印度)。
我的 html 看起来:
<div leaflet style="height: 800px; width: 100%"
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
[leafletBaseLayers]="baseLayers"
[leafletLayersControlOptions]="layersControlOptions"
[leafletMarkerCluster]="markerClusterData"
[leafletMarkerClusterOptions]="markerClusterOptions"
(leafletMarkerClusterReady)="markerClusterReady($event)">
</div>
在 .ts
@Component({
templateUrl: 'details.component.html'
})
export class DetailsComponent implements OnInit {
LAYER_OSM = {
enabled: false,
layer: L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
maxZoom: 17,
minZoom: 2,
subdomains: ['mt0', 'mt1', 'mt2', 'mt3'],
detectRetina: true,
})
};
layersControlOptions = { position: 'bottomright' };
baseLayers = {
'Google': this.LAYER_OSM.layer
};
options = {
zoom: 3,
center: L.latLng([0.0, 0.0])
};
markerClusterGroup: L.MarkerClusterGroup;
markerClusterData: any[] = [];
markerClusterOptions: L.MarkerClusterGroupOptions = {};
fitBounds: any = null; // = [[46.67, -122.25], [47.01, -121.302]];
fGroup = L.featureGroup();
constructor(private mapDataService: MapDataService) { }
ngOnInit() {
this.mapDataService.getMapData(this.nav)
.subscribe(res => { this.generateData(res) });
}
onMapReady(map: L.Map) {
map.fitBounds(map.getBounds(),{
maxZoom: 12,
animate: true
});
}
markerClusterReady(group: L.MarkerClusterGroup) {
this.markerClusterGroup = L.markerClusterGroup();
}
generateData(res: any[]) {
const data: any[] = [];
res.forEach(function (item) {
const icon = L.icon({
iconSize: [25, 41],
iconAnchor: [13, 41],
iconRetinaUrl: 'leaflet/marker-icon-2x.png',
iconUrl: 'leaflet/marker-icon.png',
shadowUrl: 'leaflet/marker-shadow.png'
});
data.push(L.marker([item.latitude, item.longitude], { icon }));
})
this.markerClusterData = data;
}
}
onMapReady(map: L.Map), map.getBounds() 总是返回一个协调,但不是我需要的那个。
我也尝试过使用 [leafletFitBounds],但我无法获得正确的反弹。首先是现在的情况。我想要达到的第二个。
提前致谢。
this.markerClusterData
看起来像这样