Google maps 谷歌地图缩放以适应

Google maps 谷歌地图缩放以适应,google-maps,zooming,Google Maps,Zooming,我已经开发了一个地图,它使用了一个使用集群标记示例的数据库中的地理坐标。我曾尝试使用LatLngBounds()使其自动缩放 函数初始化(){ var center=newgoogle.maps.LatLng(9.4419,9.1419); var map=new google.maps.map(document.getElementById('map'){ 缩放:2, 中心:中心,, mapTypeId:google.maps.mapTypeId.ROADMAP }); var标记=[];

我已经开发了一个地图,它使用了一个使用集群标记示例的数据库中的地理坐标。我曾尝试使用
LatLngBounds()使其自动缩放


函数初始化(){
var center=newgoogle.maps.LatLng(9.4419,9.1419);
var map=new google.maps.map(document.getElementById('map'){
缩放:2,
中心:中心,,
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var标记=[];
//在for boucle之外创建信息窗口
var infoWindow=new google.maps.infoWindow;
var bounds=new google.maps.LatLngBounds();
对于(变量i=0;i
  • 使边界全球化
  • 然后

var latLng=新的google.maps.latLng(latLng.lat,latLng.lng)

编辑:正如Marcelo指出的,将此更改为:


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

加:


边界扩展(latLng)

  • 之后

var markerCluster=新的MarkerClusterer(地图、标记);

加:


映射边界(bounds);

如果将Javascript与PHP(?)或任何语言分离,您可能会注意到您正在创建一个LatLngBounds对象,但您从未使用过它。尝试查看与浏览器看到的相同内容。:-)我已经删除了这里的php代码,你有什么建议吗?请问您是否测试了您发布的Javascript?它不可能运行而不崩溃。latlng的定义在哪里?它是一个数组吗?如果是,则它没有名为latlng.lat和latlng.lng的属性。浏览器是否没有提供一些有用的错误消息?(使用适当的浏览器,而不是IE):-)
 <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>