After calling fitBounds() to recenter and zoom the map to fit the bounds, I expected getBounds() to return a valid bound. However, it is returning nil.
Below is the code snippet that reproduces this issue:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
<style>
#map {
width: 800px;
height: 400px;
}
</style>
</head>
<body>
<div id='map'></div>
<script>
var myLatlng1 = new google.maps.LatLng(-38.397, 150.644);
var myLatlng2 = new google.maps.LatLng(-34.897, 150.844);
var myLatLngBounds = new google.maps.LatLngBounds(myLatlng1, myLatlng2);
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: new google.maps.LatLng(0, 0),
zoom: 0
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
map.fitBounds(myLatLngBounds);
console.log(map.getMapTypeId());
console.log(map.getZoom());
console.log(map.getBounds());
</script>
</body>
</html>
Is there something I'm missing here? I couldn't find any relevant information in the documentation. The closest I found is a note on getBounds stating:
If the map is not yet initialized (i.e. the mapType is still null), or center and zoom have not been set then the result is null.
Additionally, getZoom also returns undefined. Does fitBounds() not set this value?
EDIT I have made updates to the code by adding a default zoom and center, following Marcelo's suggestions.