最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps map.getBounds() immediately after a call to map.fitBounds - Stack Overflow

programmeradmin0浏览0评论

I'm calling getBounds() directly after a call to fitBounds(), and I thought I'd get a valid bound back as the map recenters and zooms to fit the bounds. Unfortunately, getBounds() is returning nil.

The code to reproduce this is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".dtd"> 
<html xmlns="" xmlns:v="urn:schemas-microsoft-com:vml"> 
    <head> 
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
        <script type="text/javascript" src=""></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? I haven't been able to find anything in the docs about this situation. The nearest I get is a note on getBounds that says:

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.

Note that getZoom returns undefined, too. Does fitBounds() not set this value?

EDIT I've updated the code with a default zoom and center, as per Marcelo's suggestions.

I'm calling getBounds() directly after a call to fitBounds(), and I thought I'd get a valid bound back as the map recenters and zooms to fit the bounds. Unfortunately, getBounds() is returning nil.

The code to reproduce this is as follows:

<!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? I haven't been able to find anything in the docs about this situation. The nearest I get is a note on getBounds that says:

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.

Note that getZoom returns undefined, too. Does fitBounds() not set this value?

EDIT I've updated the code with a default zoom and center, as per Marcelo's suggestions.

Share Improve this question edited Aug 18, 2010 at 10:13 Daniel Vassallo 344k72 gold badges512 silver badges446 bronze badges asked Aug 17, 2010 at 19:10 CodebeefCodebeef 44k23 gold badges89 silver badges119 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 8

As @CrazyEnigma suggested, you can get the bounds after the bounds_changed event is triggered:

map.fitBounds(myLatLngBounds);

google.maps.event.addListener(map, 'bounds_changed', function() {
  console.log(map.getMapTypeId());
  console.log(map.getZoom());
  console.log(map.getBounds());
});

The above would print the following to your console:

roadmap
6
Object

Note that if you only want to getBounds and do something once, you should replace addListener with addListenerOnce (Thanks, @Tomas).

You will have to get the bounds after the bounds_changed event.

Use addListenerOnce when tilesloaded

google.maps.event.addListenerOnce(map, 'tilesloaded', function(){ 
map.setCenter(myLatLngBounds)
});

The answer is in your own quote. You have to set the center and zoom for the map to be initialized.

Note that in the docs for the MapOptions object specification, the values of center and zoom are marked as required:

http://code.google.com/apis/maps/documentation/javascript/reference.html#MapOptions

发布评论

评论列表(0)

  1. 暂无评论