Google地图缩放以适​​应

时间:2012-07-03 11:25:57

标签: google-maps zoom

我开发了一个地图,它使用群集标记示例来使用数据库中的地理坐标。我尝试使用LatLngBounds();

自动缩放
 <script type="text/javascript">
     function initialize() {
            var center = new google.maps.LatLng(9.4419, 9.1419);

            var map = new google.maps.Map(document.getElementById('map'), {
              zoom: 2,
              center: center,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            });

            var markers = [];
            // create the infowindow out of the for boucle
            var infoWindow = new google.maps.InfoWindow;
            var bounds = new google.maps.LatLngBounds();

for ( var i = 0; i < latlng.length; i++ ) {

      var latLng = new google.maps.LatLng(latlng.lat,latlng.lng);   

     var html = 'test show in infowondow';

    var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            icon:"/img/icon1.jpg",
            title:"test title",
        });
    // call to the function....
    bindInfoWindow(marker, map, infoWindow, html);
    markers.push(marker);

  }     

            var markerCluster = new MarkerClusterer(map, markers);
          }

    // create a function bindInfoWindow

    function bindInfoWindow(marker, map, infoWindow, html) {
          google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
          });
    }

            var markerCluster = new MarkerClusterer(map, markers);
          }
          google.maps.event.addDomListener(window, 'load', initialize);
        </script>

1 个答案:

答案 0 :(得分:1)

  • 使界限全球化。
  • 然后

var latLng = new google.maps.LatLng(latlng.lat,latlng.lng);

编辑:正如马塞洛指出的那样改变:

var latLng = new google.maps.LatLng(latlng[i].lat,latlng[i].lng);

添加:

bounds.extend(latLng)

  • 之后

var markerCluster = new MarkerClusterer(map, markers);

添加:

map.fitBounds(bounds);