gpt4 book ai didi

javascript - 相当于 gmaps api 3 中的 getBoundsZoomLevel()

转载 作者:行者123 更新时间:2023-12-02 23:50:44 25 4
gpt4 key购买 nike

在 API v2 中, map 对象有一个方便的方法 getBoundsZoomLevel() 。我用它来获得最适合边界的缩放级别,然后以某种方式操纵这个最佳缩放级别,最后设置所需的缩放级别。

我在 API v3 中找不到类似的功能。 (从 v2 迁移到 v3 时的经历是多么令人沮丧)

我真的必须使用 map.fitBounds() , map.getZoom() ,操纵和 setZoom()再次?实在是太蠢了!

最佳答案

下面是我实现的一个功能:

/**
* 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;
}

关于javascript - 相当于 gmaps api 3 中的 getBoundsZoomLevel(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com