本文介绍了gmaps api中getBoundsZoomLevel()的等价物3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在API v2中,地图对象具有方便的方法。我用它来获得最适合边界的缩放级别,然后以某种方式操作这个最佳缩放级别,最后设置所需的缩放级别。
In API v2, the map object had a handy method getBoundsZoomLevel(). I used it to get the zoom level which fits the bounds best, then manipulated this optimal zoom level somehow and finally set the desired zoom level.
我找不到类似的功能。 (从v2移动到v3时,这是一个令人沮丧的经历)
I cannot find similar function in API v3. (What a continuous frustrating experience when moving from v2 to v3)
我真的必须使用 map.fitBounds()
, map.getZoom()
,操作并再次 setZoom()
?这真是太蠢了!
Do I really have to use map.fitBounds()
, map.getZoom()
, manipulate and setZoom()
again? That's really stupid!
推荐答案
下面是我实现的一个函数:
Below is a function I have implemented:
/**
* Returns the zoom level at which the given rectangular region fits in the map view.
* The zoom level is computed for the currently selected map type.
* @param {google.maps.Map} map
* @param {google.maps.LatLngBounds} bounds
* @return {Number} zoom level
**/
function getZoomByBounds( map, bounds ){
var MAX_ZOOM = map.mapTypes.get( map.getMapTypeId() ).maxZoom || 21 ;
var MIN_ZOOM = map.mapTypes.get( map.getMapTypeId() ).minZoom || 0 ;
var ne= map.getProjection().fromLatLngToPoint( bounds.getNorthEast() );
var sw= map.getProjection().fromLatLngToPoint( bounds.getSouthWest() );
var worldCoordWidth = Math.abs(ne.x-sw.x);
var worldCoordHeight = Math.abs(ne.y-sw.y);
//Fit padding in pixels
var FIT_PAD = 40;
for( var zoom = MAX_ZOOM; zoom >= MIN_ZOOM; --zoom ){
if( worldCoordWidth*(1<<zoom)+2*FIT_PAD < $(map.getDiv()).width() &&
worldCoordHeight*(1<<zoom)+2*FIT_PAD < $(map.getDiv()).height() )
return zoom;
}
return 0;
}
这篇关于gmaps api中getBoundsZoomLevel()的等价物3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!