谷歌地图V3问题与中心多个标记和自动缩放



我需要知道如何居中所有的标记和自动缩放地图,以适合他们都在我下面的地图。为了解决这个问题,我这几天都在绞尽脑汁,请帮帮我吧。

我的代码
<script type="text/javascript">  
  var map;
  var address = new Array();
  //we can also create following array with a for loop
  address[0] = '798 9th Ave, New York, NY';
  address[1] = '42 E 29th St, New York, NY';
  address[2] = '56 W 25th St, New York, NY';
  address[3] = '26 W 21th St, New York, NY';
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {zoom: 15, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);    
    for (var i=0; i<=address.length; i++) {     
        geocoder.geocode({'address': address[i]}, function(results, status) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({map: map, position: results[0].geometry.location});      
        });
    }   
  }  
</script>
<script type="text/javascript">  
  var map;
  var address = new Array();
  //we can also create following array with a for loop
  address[0] = '798 9th Ave, New York, NY';
  address[1] = '42 E 29th St, New York, NY';
  address[2] = '56 W 25th St, New York, NY';
  address[3] = '26 W 21th St, New York, NY';
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {zoom: 15, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP}
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    //Mod        
    var bounds = new google.maps.LatLngBounds();
    for (var i=0; i<=address.length; i++) {     
        geocoder.geocode({'address': address[i]}, function(results, status) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({map: map, position: results[0].geometry.location});  
            //Mod
            bounds.extend(marker.getPosition());
        });
    } 
    //Mod
    map.fitBounds(bounds);
  }  
</script>

最新更新