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

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

问题描述

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

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()) );

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

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?

推荐答案

编辑:见下方 Matt Diamond 的评论.

Edit: See Matt Diamond's comment below.

明白了!试试这个:

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 之后 setZoom 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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