Ale*_*ein 5 javascript google-maps google-maps-api-3
我试图找到一种方法来确定地图的缩放级别,然后我调用map.fitBounds(),我似乎无法找到方法.
在API v2中有一个方法GMap.getBoundsZoomLevel(bounds:GLatLngBounds),但我在API v3文档中找不到相应的方法.
是否有非谷歌的算法可以预先确定缩放级别?
Elr*_*oid 14
尼克是对的,这个讨论概述了一种可行的方法: 谷歌地图V3 - 如何计算给定边界的缩放级别
但是,它是在Javascript中.对于需要使用Android GMaps v3执行此操作的用户,以下是翻译:
private static final double LN2 = 0.6931471805599453;
private static final int WORLD_PX_HEIGHT = 256;
private static final int WORLD_PX_WIDTH = 256;
private static final int ZOOM_MAX = 21;
public int getBoundsZoomLevel(LatLngBounds bounds, int mapWidthPx, int mapHeightPx){
LatLng ne = bounds.northeast;
LatLng sw = bounds.southwest;
double latFraction = (latRad(ne.latitude) - latRad(sw.latitude)) / Math.PI;
double lngDiff = ne.longitude - sw.longitude;
double lngFraction = ((lngDiff < 0) ? (lngDiff + 360) : lngDiff) / 360;
double latZoom = zoom(mapHeightPx, WORLD_PX_HEIGHT, latFraction);
double lngZoom = zoom(mapWidthPx, WORLD_PX_WIDTH, lngFraction);
int result = Math.min((int)latZoom, (int)lngZoom);
return Math.min(result, ZOOM_MAX);
}
private double latRad(double lat) {
double sin = Math.sin(lat * Math.PI / 180);
double radX2 = Math.log((1 + sin) / (1 - sin)) / 2;
return Math.max(Math.min(radX2, Math.PI), -Math.PI) / 2;
}
private double zoom(int mapPx, int worldPx, double fraction) {
return Math.floor(Math.log(mapPx / worldPx / fraction) / LN2);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6202 次 |
| 最近记录: |