谷歌地图Api版本3自动缩放

时间:2013-11-05 11:24:25

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

我有一个问题我是谷歌地图的初学者,但在PHP脚本中却不是很好。是否存在“自动缩放”功能的可能性解决方案。我在地图上有不同的标记,我想将自动缩放到每个标记为Visibel的级别?

有没有解决方案请帮助我!

好这是我的剧本:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCxuRdCmN2BO5PD7kwI0mO8JflcUOF4CtI&sensor=true">

</script>
<script type="text/javascript">
    function initialize() {
        var mapOptions = {
            center: new google.maps.LatLng(50.903033, 10.496063),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
        mapOptions);
        var markerBounds = new google.maps.LatLngBounds();
        var contentString;
        <? php
        $i = 1;
        foreach($FertigID as $ID) {
            $result = mysql_query("SELECT * FROM Daten_CH WHERE ID = ".$ID."");
            while ($Ergebnisse = mysql_fetch_array($result)) { // Werden alle ergebnisse (ID´s) in einen Array geschriebenn 
                if (isset($Ergebnisse["GIS_y"]) && $Ergebnisse["GIS_y"] != "" && $Ergebnisse["GIS_y"] != " ") { ?> contentString = '<a href="Change.php?ID=<?php echo $ID; ?>"><input type="button" value="BEARBEITEN"></button></a>';
    var Position = new google.maps.LatLng( <? php echo $Ergebnisse["GIS_y"]; ?> , <? php echo $Ergebnisse["GIS_x"]; ?> );
                    var infowindow = new google.maps.InfoWindow({
                        content: contentString
                    });
                    var marker <? php echo $i; ?> = new google.maps.Marker({
                        position: Position,
                        map: map,
                        title: '<?php echo $Ergebnisse["AA_Objektname"]; ?>'
                    });
                    google.maps.event.addListener(marker <? php echo $i; ?> , 'click', function() {
                        infowindow.open(map, marker <? php echo $i; ?> );
                    });
                    <? php
                }
            }

            $i++;

        } ?>
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

1 个答案:

答案 0 :(得分:9)

您要做的是创建一个LatLngBounds对象,使用其extend方法扩展您的标记边界(使用LatLng个对象),然后使用{{1 } {} fitBounds如果您想要动画Map对象,地图会自动缩放到显示所有标记的位置。

这是一些基本的伪代码:

panToBounds

这是你的代码:

// You create your map. You have to do this anyway :)
// The Map object has a lot of options, you specify them according to your needs.
var map = new google.maps.Map(document.getElementById('map'), {
    'param1': 'value1',
    'param2': 'value2'
});

// Create a new LatLngBounds object
var markerBounds = new google.maps.LatLngBounds();

// Add your points to the LatLngBounds object.
foreach(marker in markers) {
    var point = new google.maps.LatLng(marker.lat, marker.lng);
    markerBounds.extend(point);
}

// Then you just call the fitBounds method and the Maps widget does all rest.
map.fitBounds(markerBounds);