如何使用LatLng数据放大LatLngBounds框

时间:2015-02-24 19:52:07

标签: javascript html5 google-maps google-maps-api-3

我通过调用以下方式获取谷歌地图的当前界限:

map.getBounds()

这将返回LatLngBounds。例如:

((-26.574013562724005, -1.5375947819336488), (-25.230016040794602, 3.735842718066351))

如果我想在地图中心周围增加一个百分比(例如1%),是否有可能获得新的LatLng点。

我尝试将每个坐标乘以1.01但这会改变地图并且不会增加界限。

我想使用增加的框大小来启动缓存标记,以便在用户平移或缩小时保存对服务器的调用。

下面的图片将更好地解释需要发生的事情:

enter image description here

1 个答案:

答案 0 :(得分:2)

基于markerclusterer getExtendedBounds功能,您可以使用此功能:

function getExtendedBounds(map, overlay) {
    //With _mapBoundsEnlargePixels we add some extra space surrounding the map
    //change this value until you get the desired size
    var _mapBoundsEnlargePixels = 500;

    var projection = overlay.getProjection();
    var bounds = angular.copy(map.getBounds());

    // Turn the bounds into latlng.
    var tr = new google.maps.LatLng(bounds.getNorthEast().lat(),
        bounds.getNorthEast().lng());
    var bl = new google.maps.LatLng(bounds.getSouthWest().lat(),
        bounds.getSouthWest().lng());

    // Convert the points to pixels and extend out
    var trPix = projection.fromLatLngToDivPixel(tr);
    trPix.x = trPix.x + _mapBoundsEnlargePixels;
    trPix.y = trPix.y - _mapBoundsEnlargePixels;

    var blPix = projection.fromLatLngToDivPixel(bl);
    blPix.x = blPix.x - _mapBoundsEnlargePixels;
    blPix.y = blPix.y + _mapBoundsEnlargePixels;

    // Convert the pixel points back to LatLng
    var ne = projection.fromDivPixelToLatLng(trPix);
    var sw = projection.fromDivPixelToLatLng(blPix);

    // Extend the bounds to contain the new bounds.
    bounds.extend(ne);
    bounds.extend(sw);

    return bounds;
}