I am currently utilizing the fitbound function to automatically zoom in on a polyline displayed on a map. However, I have noticed that on the initial attempt, it does not work as expected. But if I make changes to the chartData and then call the same function again, it works perfectly fine. Here is the code snippet for the function:
var mapOptions = {
zoom: 1,
center: { lat: 0, lng: 0 },
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL
},
mapTypeId: 'satellite'
};
this.chart = new google.maps.Map(document.getElementById(chart_element.id), mapOptions);
let polylinePath = new google.maps.Polyline({
path: chart_data,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
/* set auto zoom level to polyline */
var latlngbounds = new google.maps.LatLngBounds();
for (var i = 0; i < this.chartData.length; i++) {
latlngbounds.extend(this.chartData[i]);
}
this.chart.fitBounds(latlngbounds);
/* Set polyline chart to map */
polylinePath.setMap(this.chart);