Google地图v3:强制分钟。使用fitBounds时的缩放级别 [英] Google Maps v3: Enforcing min. zoom level when using fitBounds

查看:148
本文介绍了Google地图v3:强制分钟。使用fitBounds时的缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图上绘制了一系列标记(使用map api的v3)。



在v2中,我有以下代码:

  bounds = new GLatLngBounds(); 

...循环并在地图上放置标记...
bounds.extend(point);
...结束循环

map.setCenter(bounds.getCenter());
var level = map.getBoundsZoomLevel(bounds);
if(level == 1)
level = 5;
map.setZoom(level> 6?6:level);

这样做很好,以确保地图上始终显示适当的细节等级。 / p>

我试图在v3中复制此功能,但setZoom和fitBounds似乎没有合作关系:

  ...循环并在地图上放置标记
var ll = new google.maps.LatLng(p.lat,p.lng);
bounds.extend(ll);
...结束循环

var zoom = map.getZoom();
map.setZoom(zoom> 6?6:zoom);
map.fitBounds(bounds);

我尝试了不同的排列方式(例如在setZoom之前移动fitBounds)与setZoom似乎影响地图。我错过了什么吗?有没有办法做到这一点?

解决方案

我恰好在寻找同样的东西:)



在关于 Google Groups 我发现基本上当你做一个fitBounds时,缩放是异步发生的,所以你需要捕捉缩放和边界改变事件。最后一篇文章中的代码对我进行了小小的修改......因为它阻止了你完全缩放超过15个,所以使用第四篇文章中的想法来设置一个标志只在第一次完成。 / p>

  //执行其他操作来设置地图
var map = new google.maps.Map(mapElement,myOptions) ;
//这需要在fitbounds后设置缩放,
google.maps.event.addListener(map,'zoom_changed',function(){
zoomChangeBoundsListener =
google。 maps.event.addListener(map,'bounds_changed',function(event){
if(this.getZoom()> 15&& this.initialZoom == true){
// Change最大/最小缩放放大
this.setZoom(15);
this.initialZoom = false;
}
google.maps.event.removeListener(zoomChangeBoundsListener);
});
});
map.initialZoom = true;
map.fitBounds(bounds);

希望有帮助,

Anthony。

I'm drawing a series of markers on a map (using v3 of the maps api).

In v2, I had the following code:

  bounds = new GLatLngBounds();

  ... loop thru and put markers on map ...
  bounds.extend(point);
  ... end looping

  map.setCenter(bounds.getCenter());
  var level = map.getBoundsZoomLevel(bounds);
  if ( level == 1 ) 
    level = 5;
  map.setZoom(level > 6 ? 6 : level);

And that work fine to ensure that there was always an appropriate level of detail displayed on the map.

I'm trying to duplicate this functionality in v3, but the setZoom and fitBounds don't seem to be cooperating:

... loop thru and put markers on the map
  var ll = new google.maps.LatLng(p.lat,p.lng);
  bounds.extend(ll);
... end loop

var zoom = map.getZoom();
map.setZoom(zoom > 6 ? 6 : zoom);
map.fitBounds(bounds);

I've tried different permutation (moving the fitBounds before the setZoom, for example) but nothing I do with setZoom seems to affect the map. Am I missing something? Is there a way to do this?

解决方案

I happened to be looking for the same thing :)

At this discussion on Google Groups I discovered that basically when you do a fitBounds, the zoom happens asynchronously so you need to capture the zoom and bounds change event. The code in the final post worked for me with a small modification... as it stands it stops you zooming greater than 15 completely, so used the idea from the fourth post to have a flag set to only do it the first time.

// Do other stuff to set up map
var map = new google.maps.Map(mapElement, myOptions);
// This is needed to set the zoom after fitbounds, 
google.maps.event.addListener(map, 'zoom_changed', function() {
    zoomChangeBoundsListener = 
        google.maps.event.addListener(map, 'bounds_changed', function(event) {
            if (this.getZoom() > 15 && this.initialZoom == true) {
                // Change max/min zoom here
                this.setZoom(15);
                this.initialZoom = false;
            }
        google.maps.event.removeListener(zoomChangeBoundsListener);
    });
});
map.initialZoom = true;
map.fitBounds(bounds);

Hope that helps,

Anthony.

这篇关于Google地图v3:强制分钟。使用fitBounds时的缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆