6

我正在尝试设置地图 fitbounds 的边界不起作用,因为它在边界周围放置了一些空间,因此这样做

{{{map.fitBounds(map.getBounds())}}}

几次将快速缩小地图

我需要能够做到

{{{map.setBounds(map.getBounds())}}}

什么都不会发生

请注意,我使用的是 v3 的 api,因此无法访问 getBoundsZoomLevel

4

3 回答 3

4

如果我没记错的话,我假设您希望您的所有点都以尽可能高的缩放级别在地图上可见。我通过将地图的缩放级别初始化为 16 来实现这一点(不确定它是否是 V3 上可能的最高缩放级别)。

var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 16
                                        , center: marker_point
                                        , mapTypeId: google.maps.MapTypeId.ROADMAP
});

然后在那之后我做了边界的东西:

var bounds = new google.maps.LatLngBounds();

//you can have a loop here of all you marker points
//begin loop
bounds.extend(marker_point);
//end loop

map.fitBounds(bounds);

结果:成功!

于 2010-08-09T23:25:16.393 回答
0

FitBounds就像你说的扩展边界以超过第二张地图的边界,为了使边界完全相同,你可以设置中心和缩放。

map2.setCenter(map.getCenter());
map2.setZoom(map.getZoom());

正如在这个 JSFiddle - http://jsfiddle.net/KmNaX/

于 2013-09-11T17:48:07.327 回答
0

嘿,Guyzz,当我没有任何工作时,我计划采用这种方法..它对我有用..我从视口获取坐标,然后将其映射到边界..这种方法有效..

function codeAddress() {
    var address = document.getElementById('address').value;
    if(address == null || address== "") {
        alert('Address field is Empty.. !');
    } else {
        geocoder.geocode({
            'address' : address
        }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                // If geocoder status is OK then locate the geometry,..
                var location = results[0].geometry.location
                // Obtaining the lat lang of new position on map.. 
                var latlng = new google.maps.LatLng(location.lat(),location.lng());

                map.setCenter(location);

                // view port gives us the map bounds for a particular location.. 
                **var viewportloc = String(results[0].geometry.viewport);
                var res = viewportloc.replace('((','').replace('))','').replace('(','').replace(')','').split(',');

                //alert(res[0]+" - "+res[1]+" - "+res[2]+" - "+res[3]);
                var sw = new google.maps.LatLng(res[0],res[1]);
                var ne = new google.maps.LatLng(res[2],res[3]);

                var bounds = new google.maps.LatLngBounds();
                bounds.extend(sw);
                bounds.extend(ne);

                map.fitBounds(bounds);**

                addMarker(location);
                setonMap(map);

            } else {
                document.getElementById('address').value = "";
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
}
于 2015-08-12T04:13:29.723 回答