Рассчитать границы от центра и масштаба (Google Maps API v3) - PullRequest
3 голосов
/ 23 сентября 2010

Мне нужно рассчитать границы карты с учетом центра и уровня масштабирования.

Я полагаю, что мог бы временно установить центр карты и масштабирование и вызвать map.getBounds(), но я бы предпочел этого не делать (мне нужно было бы отключить / повторно включить несколько обработчиков событий).

Кто-нибудь знает, как это сделать в v3?

Ответы [ 2 ]

9 голосов
/ 04 ноября 2010

При этом используется немного mootools, поэтому вам придется редактировать код, чтобы использовать его с другими библиотеками (должно быть достаточно просто).

/**
 * 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)));
}
0 голосов
/ 11 октября 2010

Какой ваш вариант использования здесь?

Вы можете рассчитать это в API v2:

map.getBoundsZoomLevel(bounds);

Однако вAPI v3.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...