I have an issue with Google Map in javascript, I can not make all markers fit bounds. Only the first marker is visible. This is the error thrown in console:
uncaught exception: InvalidValueError: not a LatLngBounds or LatLngBoundsLiteral: not an Object
And this is the code I am using:
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: parseFloat(lat), lng:parseFloat(lng)},
zoom: 11
});
var bounds = new google.maps.LatLngBounds();
for(var i = 0; i < coordinates.length; i++){
var coords = {lat: parseFloat(lat), lng:parseFloat(lng)}
var marker = new google.maps.Marker({position: coords, map: map});
var infowindow = new google.maps.InfoWindow();
infowindow.setContent(lat);
infowindow.open(map, marker);
bounds.extend(marker.position);
}
map.setCenter(bounds.getCenter());
map.fitBounds(bounds);
I have 4 markers on the map, but map is zoomed and centered to the last one added, as seen in the image below:
If I zoom out the map I can see all other markers, but fitbounds is not working to make all markers fit the bounds.