根据 Google 地图结果进行缩放

Zooming based on Google Maps result

这是我当前的代码:

$scope.search = function(){
    Request.googleAPI($scope.searchForm).then(function(data){
        console.log("Search result: ")
        console.log(data)
        var actualZoom = map.getView().getZoom();
        console.log("Zoom level: " + actualZoom)
        var point = [data.data.results[0].geometry.location.lng, data.data.results[0].geometry.location.lat];
        var size = /** @type {ol.Size} */ (map.getSize());
        view.centerOn(ol.proj.transform(point, 'EPSG:4326', 'EPSG:3857'), size, [size[0]/2, size[1]/2]);
        /*
        map.setView(new ol.View({
            projection: 'EPSG:4326',
            center: [long, lat], //long,lat
            zoom: actualZoom
        }));
        */
    })
}

我试过使用底部评论的内容(SO 上的建议)。我将其调整为仅使用 "zoom",但它会使我的地图变白。

Google 地图请求包含几何点位置 (lat/lng),但也包含应确定缩放级别的边界框。如何使用此边界框正确更新我的地图?

如果您需要查看更多代码,这里是我正在使用的 complete script

解决了我的数学问题!

$scope.search = function(){
    Request.googleAPI($scope.searchForm).then(function(data){

        var point = [data.data.results[0].geometry.location.lng, data.data.results[0].geometry.location.lat];
        var size = /** @type {ol.Size} */ (map.getSize());
        view.centerOn(ol.proj.transform(point, 'EPSG:4326', 'EPSG:3857'), size, [size[0]/2, size[1]/2]);

        // To adjust Zoom Level
        var bound = data.data.results[0].geometry.viewport;
        var east = bound.northeast.lng
        var west = bound.southwest.lng
        var zoom = 9 - Math.floor(Math.log(Math.abs(west-east))/Math.log(2));
        map.getView().setZoom(zoom);
    })
}