谷歌地图 fitBounds 带填充? [复制]

2024-06-28

我们想要这样的东西传单 fitBounds https://stackoverflow.com/questions/19407985/leaflet-js-fitbounds-with-padding在我们的谷歌地图应用程序中。我们看到了一些例子,但它没有按预期工作。我们尝试将 div 作为谷歌地图控制器,手动扩展边界,但没有任何效果......

function zoomToBounds(polygon){
bounds = new google.maps.LatLngBounds();
$.each(polygon, function( key, latLng ) {
    bounds.extend(new google.maps.LatLng(latLng.lat, latLng.lng));
});    

/////////////////////////////////////////////////////////////
// We saw a solution like this, but the zooming is a problem
//
// Extend bounds with a fake point:
/////////////////////////////////////////////////////////////
/*
if (this.map.getProjection()) {
    proj = this.map.getProjection();
    latlng = bounds.getSouthWest();
    swPoint = proj.fromLatLngToPoint(latlng);
    latlng2 = proj.fromPointToLatLng({
      x: swPoint.x - 10,
      y: swPoint.y
    });
    bounds.extend(latlng2);
}*/

this.map.fitBounds(bounds);
}

一段时间后,我找到了一个有效的解决方案。

if (this.map.getProjection()) {
    var offsetx = 200;
    var offsety = 0;

    var point1 = this.map.getProjection().fromLatLngToPoint(bounds.getSouthWest());
    this.map.fitBounds(bounds);

    var point2 = new google.maps.Point(
        ( (typeof(offsetx) == 'number' ? offsetx : 0) / Math.pow(2, this.map.getZoom()) ) || 0,
        ( (typeof(offsety) == 'number' ? offsety : 0) / Math.pow(2, this.map.getZoom()) ) || 0
    );          

    var newPoint = this.map.getProjection().fromPointToLatLng(new google.maps.Point(
        point1.x - point2.x,
        point1.y + point2.y
    ));

    bounds.extend(newPoint);
    this.map.fitBounds(bounds);
}

jsfiddle http://jsfiddle.net/erzzo/g7br74a4/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

谷歌地图 fitBounds 带填充? [复制] 的相关文章

随机推荐