简体   繁体   English

动态设置谷歌地图缩放

[英]Set google map zoom dynamically

It's possible to define a level of zoom depending of the height of polygon shapes (administrative boundaries)?是否可以根据多边形形状(管理边界)的高度定义缩放级别? This polygon going to be completely displayed into the map's div.这个多边形将完全显示在地图的 div 中。

There is a map.fitBounds(my_bounds) function, which sets the viewport to contain the given bounds.有一个map.fitBounds(my_bounds)函数,它设置视口以包含给定的边界。 It would set the map to the highest zoom level possible, in which you can see the whole bounds.它会将地图设置为可能的最高缩放级别,您可以在其中看到整个边界。

For that you need to determine the bounds of your polygon.为此,您需要确定多边形的边界。 If you use google.maps.Polygon to display your polygons, check out this solution .如果您使用google.maps.Polygon显示多边形,请查看此解决方案

In this case you would have:在这种情况下,您将拥有:

google.maps.Polygon.prototype.getBounds = function() {
    var bounds = new google.maps.LatLngBounds();
    var paths = this.getPaths();
    var path;        
    for (var i = 0; i < paths.getLength(); i++) {
        path = paths.getAt(i);
        for (var ii = 0; ii < path.getLength(); ii++) {
            bounds.extend(path.getAt(ii));
        }
    }
    return bounds;
}

...
map.fitBounds(my_polygon.getBounds());

If you use Data layer to display polygons, check out this SO answer .如果您使用Data layer显示多边形,请查看此 SO 答案 It provides example on how to calculate bounds of Data.Feature if it's a polygon.它提供了有关如何计算Data.Feature边界(如果它是多边形)的Data.Feature

If you have some other means of determining the bounds of polygon, then just use this:如果您有其他确定多边形边界的方法,则只需使用以下方法:

map.fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(SouthWestCornerLat, SouthWestCornerLng), new google.maps.LatLng(NorthEastCornerLat, NorthEastCornerLng));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM