我有一组点,我想在嵌入式谷歌地图(API v3)上绘制。我希望边界容纳所有的点,除非缩放级别太低(即,缩小太多)。我的方法是这样的:

var bounds = new google.maps.LatLngBounds();

// extend bounds with each point

gmap.fitBounds(bounds); 
gmap.setZoom( Math.max(6, gmap.getZoom()) );

这行不通。最后一行“gmap.setZoom()”如果在fitBounds之后直接调用,则不会改变地图的缩放级别。

有没有一种方法来获得缩放级别的边界而不应用到地图?还有其他解决方法吗?


当前回答

我看到许多不正确或太复杂的解决方案,所以决定张贴一个工作,优雅的解决方案。

setZoom()没有像你期望的那样工作的原因是fitBounds()是异步的,所以它不能保证它会立即更新缩放,但是你对setZoom()的调用依赖于这一点。

你可能会考虑在调用fitBounds()之前设置minZoom map选项,然后在调用完成后清除它(这样用户仍然可以手动缩小):

var bounds = new google.maps.LatLngBounds();
// ... (extend bounds with all points you want to fit)

// Ensure the map does not get too zoomed out when fitting the bounds.
gmap.setOptions({minZoom: 6});
// Clear the minZoom only after the map fits the bounds (note that
// fitBounds() is asynchronous). The 'idle' event fires when the map
// becomes idle after panning or zooming.
google.maps.event.addListenerOnce(gmap, 'idle', function() {
  gmap.setOptions({minZoom: null});
});

gmap.fitBounds(bounds);

此外,如果你也想限制最大缩放,你可以对maxZoom属性应用同样的技巧。

请参阅MapOptions文档。

其他回答

我有同样的问题,我能够解决它使用下面的代码。这个监听器(google.maps.addListenerOnce())事件只会在map.fitBounds()执行之后被触发一次。所以,没有必要

跟踪并手动删除侦听器,或者 等待地图空闲。

它最初设置适当的缩放级别,并允许用户放大和缩小超过初始缩放级别,因为事件侦听器已经过期。例如,如果只调用google.maps.addListener(),那么用户将永远无法放大超过指定的缩放级别(在本例中为4)。由于我们实现了google.maps.addListenerOnce(),用户将能够放大到他/她选择的任何级别。

map.fitBounds(bounds);

var zoom_level_for_one_marker = 4;

google.maps.event.addListenerOnce(map, 'bounds_changed', function(event){
   if (this.getZoom() >= zoom_level_for_one_marker){  
       this.setZoom(zoom_level_for_one_marker) 
   }
});

在这个函数中,您只需要动态添加元数据来存储几何类型,因为该函数接受任何几何。

“fitGeometries”是一个扩展映射对象的JSON函数。

"geometries"是一个通用的javascript数组,而不是MVCArray()。

geometry.metadata = { type: "point" };
var geometries = [geometry];

fitGeometries: function (geometries) {
    // go and determine the latLngBounds...
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0; i < geometries.length; i++) {
        var geometry = geometries[i];
        switch (geometry.metadata.type)
        {
            case "point":
                var point = geometry.getPosition();
                bounds.extend(point);
                break;
            case "polyline":
            case "polygon": // Will only get first path
                var path = geometry.getPath();
                for (var j = 0; j < path.getLength(); j++) {
                    var point = path.getAt(j);
                    bounds.extend(point);
                }
                break;
        }
    }
    this.getMap().fitBounds(bounds);
},

有同样的问题,需要适应地图上的许多标记。 这解决了我的问题:

申报范围 koderoid提供的使用方案(用于每个标记集bounds.extend(objLatLng)) 执行fitbounds后映射完成: google.maps.event。addListenerOnce(map, 'idle', function() { 地图。fitBounds(边界); });

像我一样,如果你不愿意和听众一起玩,这是我想出的一个简单的解决方案: 在地图上添加一个方法,严格按照你的要求工作,像这样:

    map.fitLmtdBounds = function(bounds, min, max){
        if(bounds.isEmpty()) return;
        if(typeof min == "undefined") min = 5;
        if(typeof max == "undefined") max = 15;

        var tMin = this.minZoom, tMax = this.maxZoom;
        this.setOptions({minZoom:min, maxZoom:max});
        this.fitBounds(bounds);
        this.setOptions({minZoom:tMin, maxZoom:tMax});
    }

那么你可以调用map.fitLmtdBounds(bounds)而不是map.fitBounds(bounds)来设置定义的缩放范围下的边界…或者map.fitLmtdBounds(bounds,3,5)来覆盖缩放范围..

我使用:

gmap.setZoom(24); //this looks a high enough zoom value
gmap.fitBounds(bounds); //now the fitBounds should make the zoom value only less

这将使用较小的24和必要的缩放级别根据你的代码,但它可能会改变缩放无论如何,并不关心你有多少缩小。