3

問題があります 私は Google マップの初心者で、Java Script はあまり得意ではありませんが、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>
4

1 に答える 1

9

あなたがしたいことは、LatLngBoundsオブジェクトを作成し、そのメソッドを使用して ( LatLngextendオブジェクトを使用して) マーカーで境界を拡張し、次に(またはアニメーションが必要な場合は) Mapオブジェクトを使用すると、マップが自動的にズームされます。すべてのマーカーが表示される位置。fitBoundspanToBounds

基本的な疑似コードを次に示します。

// 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>
于 2013-11-05T11:40:43.077 に答える