从中心和缩放计算边界(Google Maps API v3) [英] Calculate bounds from center and zoom (Google Maps API v3)

查看:81
本文介绍了从中心和缩放计算边界(Google Maps API v3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想我可以临时设置地图中心和缩放,并调用 map.getBounds(),但我不希望(我需要禁用/重新启用一些事件处理程序)。



有人知道如何在v3中做到这一点吗?

解决方案

这使用了一些mootools,所以你必须编辑用于其他库的代码(应该很容易)。

  / ** 
*计算边界该地图会以给定的缩放级别显示。
*
* @member google.maps.Map
* @method boundsAt
* @param {数字}缩放缩放级别用于计算。
* @param {google.maps.LatLng} [center]可以设置为指定与当前地图中心不同的中心。
* @param {google.maps.Projection} [projection]可以设置为使用与this.getProjection()返回的投影不同的投影。
* @param {Element} [div]可以设置为指定与this.getDiv()不同的地图视口(仅用于获取尺寸)。
* @return {google.maps.LatLngBounds}计算的范围。
*
* @example
* var bounds = map.boundsAt(5); //和map.boundsAt相同(5,map.getCenter(),map.getProjection(),map.getDiv());
* /
google.maps.Map.prototype.boundsAt = function(zoom,center,projection,div){
var p = projection || this.getProjection();
if(!p)return undefined;
var d = $(div || this.getDiv());
var zf = Math.pow(2,zoom)* 2;
var dw = d.getStyle('width')。toInt()/ zf;
var dh = d.getStyle('height')。toInt()/ zf;
var cpx = p.fromLatLngToPoint(center || this.getCenter());
return new google.maps.LatLngBounds(
p.fromPointToLatLng(new google.maps.Point(cpx.x - dw,cpx.y + dh)),
p.fromPointToLatLng(new google.maps.Point(cpx.x + dw,cpx.y - dh)));
}


I need to calculate the bounds of a map given center and zoom level.

I imagine I could temporarily set map center and zoom and call map.getBounds(), but I would prefer not to (I would need to disable/re-enable a few event handlers).

Does anybody know how to do this in v3?

解决方案

This uses a bit of mootools, so you'll have to edit the code to use it with other libraries (should be easy enough).

/**
 * Calculates the bounds this map would display at a given zoom level.
 *
 * @member google.maps.Map
 * @method boundsAt
 * @param {Number}                 zoom         Zoom level to use for calculation.
 * @param {google.maps.LatLng}     [center]     May be set to specify a different center than the current map center.
 * @param {google.maps.Projection} [projection] May be set to use a different projection than that returned by this.getProjection().
 * @param {Element}                [div]        May be set to specify a different map viewport than this.getDiv() (only used to get dimensions).
 * @return {google.maps.LatLngBounds} the calculated bounds.
 *
 * @example
 * var bounds = map.boundsAt(5); // same as map.boundsAt(5, map.getCenter(), map.getProjection(), map.getDiv());
 */
google.maps.Map.prototype.boundsAt = function (zoom, center, projection, div) {
    var p = projection || this.getProjection();
    if (!p) return undefined;
    var d = $(div || this.getDiv());
    var zf = Math.pow(2, zoom) * 2;
    var dw = d.getStyle('width').toInt()  / zf;
    var dh = d.getStyle('height').toInt() / zf;
    var cpx = p.fromLatLngToPoint(center || this.getCenter());
    return new google.maps.LatLngBounds(
        p.fromPointToLatLng(new google.maps.Point(cpx.x - dw, cpx.y + dh)),
        p.fromPointToLatLng(new google.maps.Point(cpx.x + dw, cpx.y - dh)));
}

这篇关于从中心和缩放计算边界(Google Maps API v3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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