Google Maps V3 - 如何计算给定边界的缩放级别 [英] Google Maps V3 - How to calculate the zoom level for a given bounds

查看:122
本文介绍了Google Maps V3 - 如何计算给定边界的缩放级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来使用Google Maps V3 API计算给定范围的缩放级别,类似于V2 API中的getBoundsZoomLevel()。



这是我想要做的:

//这些是以前从地图对象
var sw = new google.maps.LatLng(42.763479,-84.338918);
var ne = new google.maps.LatLng(42.679488,-84.524313);
var bounds = new google.maps.LatLngBounds(sw,ne);
var zoom = //做一些魔术来计算缩放级别

//将地图设置为这些确切的边界
map.setCenter(bounds.getCenter());
map.setZoom(zoom);

//注意:fitBounds()不起作用

不幸的是,我不能为我的特殊用例使用fitBounds()方法。它适用于在地图上绘制标记,但对于设置确切的边界并不适用。这里是我为什么不能使用fitBounds()方法的一个例子。

  map。 fitBounds(map.getBounds()); //不是你所期望的


解决方案

在Google小组上询问: http://groups.google.com/ group / google-maps-js-api-v3 / browse_thread / thread / e6448fc197c3c892



缩放级别是离散的,每一步缩小一倍。所以一般来说,你无法完全符合你想要的边界(除非你对特定的地图大小感到非常幸运)。

你无法准确地将边界与方形地图内的细长矩形进行匹配。



如何拟合确切的边界并不容易,因为即使您愿意更改地图div的大小,你必须选择你改变的大小和相应的缩放级别(粗略地说,你把它放大还是小于现在的尺寸?)。

<如果你真的需要计算缩放比,而不是存储它,这应该做的伎俩:

墨卡托投影翘曲的纬度,但经度的任何差异总是表示地图宽度的相同部分(以度/ 360为单位的角度差)。在缩放零点时,整个世界地图是256x256像素,每个级别的缩放倍数都是宽度和高度。因此,在一个小代数之后,我们可以按如下方式计算缩放比例,前提是我们知道地图的像素宽度。请注意,由于经度环绕,我们必须确保角度为正。

  var GLOBE_WIDTH = 256; // Google地图投影中的常量
var west = sw.lng();
var east = ne.lng();
var angle = east - west;
if(angle <0){
angle + = 360;
}
var zoom = Math.round(Math.log(pixelWidth * 360 / angle / GLOBE_WIDTH)/ Math.LN2);


I'm looking for a way to calculate the zoom level for a given bounds using the Google Maps V3 API, similar to getBoundsZoomLevel() in the V2 API.

Here is what I want to do:

// These are exact bounds previously captured from the map object
var sw = new google.maps.LatLng(42.763479, -84.338918);
var ne = new google.maps.LatLng(42.679488, -84.524313);
var bounds = new google.maps.LatLngBounds(sw, ne);
var zoom = // do some magic to calculate the zoom level

// Set the map to these exact bounds
map.setCenter(bounds.getCenter());
map.setZoom(zoom);

// NOTE: fitBounds() will not work

Unfortunately, I can't use the fitBounds() method for my particular use case. It works well for fitting markers on the map, but it does not work well for setting exact bounds. Here is an example of why I can't use the fitBounds() method.

map.fitBounds(map.getBounds()); // not what you expect

解决方案

A similar question has been asked on the Google group: http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/e6448fc197c3c892

The zoom levels are discrete, with the scale doubling in each step. So in general you cannot fit the bounds you want exactly (unless you are very lucky with the particular map size).

Another issue is the ratio between side lengths e.g. you cannot fit the bounds exactly to a thin rectangle inside a square map.

There's no easy answer for how to fit exact bounds, because even if you are willing to change the size of the map div, you have to choose which size and corresponding zoom level you change to (roughly speaking, do you make it larger or smaller than it currently is?).

If you really need to calculate the zoom, rather than store it, this should do the trick:

The Mercator projection warps latitude, but any difference in longitude always represents the same fraction of the width of the map (the angle difference in degrees / 360). At zoom zero, the whole world map is 256x256 pixels, and zooming each level doubles both width and height. So after a little algebra we can calculate the zoom as follows, provided we know the map's width in pixels. Note that because longitude wraps around, we have to make sure the angle is positive.

var GLOBE_WIDTH = 256; // a constant in Google's map projection
var west = sw.lng();
var east = ne.lng();
var angle = east - west;
if (angle < 0) {
  angle += 360;
}
var zoom = Math.round(Math.log(pixelWidth * 360 / angle / GLOBE_WIDTH) / Math.LN2);

这篇关于Google Maps V3 - 如何计算给定边界的缩放级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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