【问题标题】:Zoom and center a Google Map according to its markers (JavaScript API V3)根据标记缩放和居中谷歌地图(JavaScript API V3)
【发布时间】:2011-04-01 01:15:39
【问题描述】:

我觉得标题就够了,我什至不明白如何为 V2 和 V1 API 做到这一点:/

谢谢:)

【问题讨论】:

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


    【解决方案1】:

    正如其他答案所建议的那样,fitBounds() 方法应该可以解决问题。

    考虑以下示例,它将在美国东北部生成 10 个随机点,并应用 fitBounds() 方法:

    <!DOCTYPE html>
    <html> 
    <head> 
       <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
       <title>Google Maps LatLngBounds.extend() Demo</title> 
       <script src="http://maps.google.com/maps/api/js?sensor=false" 
               type="text/javascript"></script> 
    </head> 
    <body> 
       <div id="map" style="width: 400px; height: 300px;"></div> 
    
       <script type="text/javascript"> 
    
       var map = new google.maps.Map(document.getElementById('map'), { 
         mapTypeId: google.maps.MapTypeId.TERRAIN
       });
    
       var markerBounds = new google.maps.LatLngBounds();
    
       var randomPoint, i;
    
       for (i = 0; i < 10; i++) {
         // Generate 10 random points within North East USA
         randomPoint = new google.maps.LatLng( 39.00 + (Math.random() - 0.5) * 20, 
                                              -77.00 + (Math.random() - 0.5) * 20);
    
         // Draw a marker for each random point
         new google.maps.Marker({
           position: randomPoint, 
           map: map
         });
    
         // Extend markerBounds with each random point.
         markerBounds.extend(randomPoint);
       }
    
       // At the end markerBounds will be the smallest bounding box to contain
       // our 10 random points
    
       // Finally we can call the Map.fitBounds() method to set the map to fit
       // our markerBounds
       map.fitBounds(markerBounds);
    
       </script> 
    </body> 
    </html>
    

    多次刷新此示例,没有任何标记超出视口:

    【讨论】:

    • 感谢大家,这正是我想要的!
    【解决方案2】:

    方法如下:

    map.fitBounds(bounds);
    map.setCenter(bounds.getCenter());
    

    bounds 是坐标(标记)的数组。每次放置标记时,请执行以下操作:

    bounds.extend(currLatLng);
    

    【讨论】:

    • 我不认为 setCenter 在 fitBounds 之后是必要的
    【解决方案3】:

    中心缩放取决于单个标记

    var myCenter=new google.maps.LatLng(38.8106813,-89.9600172);
    var map;
    var marker;
    var mapProp;
    function initialize()
    {
        mapProp = {
            center:myCenter,
            zoom:8,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };
    
        map=new google.maps.Map(document.getElementById("map"),mapProp);
    
        marker=new google.maps.Marker({
            position:myCenter,
            animation:google.maps.Animation.BOUNCE,
            title:"400 St. Louis Street Edwardsville, IL 62025"
        });
    
        marker.setMap(map);
        google.maps.event.addListener(map, "zoom_changed", function() {
            map.setCenter(myCenter);
        });
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    

    【讨论】:

      【解决方案4】:

      计算正确的缩放以显示所有标记并非易事。但是有一个功能:GMap.fitBounds() - 您定义一个边界(例如,所有标记坐标的最极端点)并相应地设置地图。参见例如fitbounds() in Google maps api V3 does not fit bounds(答案!)就是一个很好的例子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-28
        • 1970-01-01
        • 1970-01-01
        • 2016-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        相关资源
        最近更新 更多