计算中心和缩放的边界(Google Maps API v3) [英] Calculate bounds from center and zoom (Google Maps API v3)
本文介绍了计算中心和缩放的边界(Google Maps API v3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要计算给定中心和缩放级别的地图的边界.
I need to calculate the bounds of a map given center and zoom level.
我想我可以临时设置地图中心和缩放并调用 map.getBounds()
,但我不想这样做(我需要禁用/重新启用一些事件处理程序).
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).
有人知道如何在 v3 中做到这一点吗?
Does anybody know how to do this in v3?
推荐答案
这使用了一些 mootools,因此您必须编辑代码才能将其与其他库一起使用(应该很容易).
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屋!
查看全文