2011-05-14 134 views
4

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

<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> 

回答

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) 
    ]; 
+0

作品,谢谢! – Nyxynyx 2011-05-14 13:34:01

3

如果要定义LatLngList你有一个错误。正确的代码应该是:

var LatLngList = new Array(); 

不:

var LatLngList = array(); 
+0

可能是OP是PHP程序员,不是吗? – 2011-05-14 07:56:18

+0

是的我主要在PHP代码:)谢谢! – Nyxynyx 2011-05-14 13:33:48