Google Maps API v3:可以在fitBounds之后设置缩放吗? [英] Google Maps API v3: Can I setZoom after fitBounds?

查看:203
本文介绍了Google Maps API v3:可以在fitBounds之后设置缩放吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

//用每个点延伸边界

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

这不起作用。如果直接在fitBounds之后调用,最后一行gmap.setZoom()不会更改地图的缩放级别。



有没有办法获得缩放级别没有将其应用于地图的边界?其他想法解决这个问题? 请参阅Matt Diamond的评论。



明白了!试试这个:

  map.fitBounds(bounds); 
var listener = google.maps.event.addListener(map,idle,function(){$ b $ if(map.getZoom()> 16)map.setZoom(16);
google.maps.event.removeListener(listener);
});

修改您的需求。


I have a set of points I want to plot on an embedded Google Map (API v3). I'd like the bounds to accommodate all points unless the zoom level is too low (i.e., zoomed out too much). My approach has been like this:

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

// extend bounds with each point

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

This doesn't work. The last line "gmap.setZoom()" doesn't change the zoom level of the map if called directly after fitBounds.

Is there a way to get the zoom level of a bounds without applying it to the map? Other ideas to solve this?

解决方案

Edit: See Matt Diamond's comment below.

Got it! Try this:

map.fitBounds(bounds);
var listener = google.maps.event.addListener(map, "idle", function() { 
  if (map.getZoom() > 16) map.setZoom(16); 
  google.maps.event.removeListener(listener); 
});

Modify to your needs.

这篇关于Google Maps API v3:可以在fitBounds之后设置缩放吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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