I am using angular 6 and ngx-leaflet. I am loading data backend, and I trying to zoom to area (in this case India) once data loaded on the map.
My html looks:
<div leaflet style="height: 800px; width: 100%"
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
[leafletBaseLayers]="baseLayers"
[leafletLayersControlOptions]="layersControlOptions"
[leafletMarkerCluster]="markerClusterData"
[leafletMarkerClusterOptions]="markerClusterOptions"
(leafletMarkerClusterReady)="markerClusterReady($event)">
</div>
In .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() is returning always one coordination, but not the one I need.
I Have also tried to use [leafletFitBounds], but I am not able to get correct bounce. The first is how it is now. The second that I am trying to achieve.
Thanks in advance.
this.markerClusterData
looks like this