google-maps - Google Maps Api 第 3 版自动缩放

标签 google-maps google-maps-api-3 zooming

我有一个问题,我是 Google map 的初学者,在 Java Script 方面不是很好,但在 PHP 方面表现不佳。 “自动缩放”功能是否有可能的解决方案。我的 map 上有不同的标记,我想自动缩放到每个标记都是 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>

最佳答案

你想做的是创建一个 LatLngBounds对象,使用它的 extend 方法用你的标记扩展边界(使用 LatLng 对象),然后使用 fitBounds (或 panToBounds 如果你想要动画)的Map对象, map 将自动缩放到显示所有标记的位置。

这是一些基本的伪代码:

// 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);

这是你的代码:

<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"]; ?>'
                    });

                    markerBounds.extend(Position); // you call this method for each Position (LatLng object)

                    google.maps.event.addListener(marker <? php echo $i; ?> , 'click', function() {
                        infowindow.open(map, marker <? php echo $i; ?> );
                    });
                    <? php
                }
            }

            $i++;

        } ?>
        map.fitBounds(markerBounds); // Then you call this method here.
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

关于google-maps - Google Maps Api 第 3 版自动缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787870/

相关文章:

javascript - 是否可以使用多张图片作为 Google Maps v3 MarkerImage?

windows-phone-7 - 带有缩放和平移功能的 Windows Phone 7 图表库?

javascript - 将数据传递给模态( Bootstrap )

java - 谷歌地图 : Multiple values in snippet

javascript - 除非刷新,否则谷歌地图无法正确显示

google-maps - Google Maps v3 InfoWindow 太宽

html - 对 div 使用百分比

ios - UIScrollView.contentSize 似乎随着缩放而增长

html - 不在 polymer 上显示位置(使用谷歌地图)

google-maps - Google 地方信息自动完成