Google Maps V3缩放以显示所有标记无效

时间:2011-05-14 02:27:47

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

我正在使用Google Maps V3 API,在地图上创建了多个标记之后,我很难让地图缩小以显示所有标记。现在,下面的代码只显示标记而不调整缩放。你能在那里找到错误吗?谢谢!

<script type="text/javascript">
    function initialize() {
        var latlng = new google.maps.LatLng(1.289566,103.847267);
        var options = {  
            zoom: 15,  
            center: latlng,  
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false
        }; 

        var map = new google.maps.Map(document.getElementById('map_canvas'), options);  
        var marker = new google.maps.Marker({  
            position: new google.maps.LatLng(1.289566,103.847267),
            map: map,
            icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|ff776b"
        }); 

        var marker = new google.maps.Marker({  
            position: new google.maps.LatLng(1.301224,103.912949),
            map: map,
            icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=2|ff776b"
        }); 

        var marker = new google.maps.Marker({  
            position: new google.maps.LatLng(1.293150,103.827164),
            map: map,
            icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=3|ff776b"
        }); 

        var LatLngList = array (
        new google.maps.LatLng(1.289566,103.847267),
        new google.maps.LatLng(1.301224,103.912949),
        new google.maps.LatLng(1.293150,103.827164)
        );

        var bounds = new google.maps.LatLngBounds();

        for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
            bounds.extend(LatLngList[i]);
        }

        map.fitBounds(bounds);

    }
</script>

2 个答案:

答案 0 :(得分:5)

此:

var LatLngList = array (
        new google.maps.LatLng(1.289566,103.847267),
        new google.maps.LatLng(1.301224,103.912949),
        new google.maps.LatLng(1.293150,103.827164)
        );

不是您在JavaScript中创建数组的方式。相反,尝试:

var LatLngList = [
    new google.maps.LatLng(1.289566,103.847267),
    new google.maps.LatLng(1.301224,103.912949),
    new google.maps.LatLng(1.293150,103.827164)
    ];

答案 1 :(得分:3)

您定义LatLngList的地方有错误。正确的代码应该是:

var LatLngList = new Array();

var LatLngList = array();