Google Maps API。居中和缩放

时间:2012-06-25 06:20:49

标签: google-maps google-maps-api-3

 $('#map_canvas').gmap().bind('init', function(ev, map) {
        if (myPosition) {
            $('#map_canvas').gmap({ 'center' : myPosition });

            $('#map_canvas').gmap('addMarker', { 'position':  myPosition, 'bounds': true, 'icon' : '../../css/images/current_location.png'}).click( function () {
                $('#map_canvas').gmap('openInfoWindow', { 'content': 'You are here!' }, this);
            });

            $('#map_canvas').gmap('option', 'zoom', 20);        
        } else {
            $('#map_canvas').gmap('option', 'zoom', 8);
        }

        $.each(markers, function(i, marker) {
            $('#map_canvas').gmap('addMarker', { 
                'position': new google.maps.LatLng(marker.lat, marker.lng), 
                'bounds': true 
            }).click(function() {
                $('#map_canvas').gmap('openInfoWindow', { 'content': marker.name }, this);
            });
        });     
    });

我想要居中并缩放到特定位置。这段代码的结果是:http://postimage.org

2 个答案:

答案 0 :(得分:0)

将每个标记的latLng添加到LatLngBounds对象,然后使用fitBounds()

Here就是一个例子。

答案 1 :(得分:0)

总而言之,我达到了我的目的,但是fitBounds()函数对我的期望造成了一些混乱。我的任务是:创建很多制造商并将它们放在地图上。如果我使用fitBounds()函数,我的地图会缩小以适合我的所有标记。现在看起来像我的地图:http://postimage.org