【问题标题】:setbounds on google maps api v3谷歌地图 api v3 上的 setbounds
【发布时间】:2011-01-28 22:20:38
【问题描述】:

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

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

几次将快速缩小地图

我需要能够做到

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

什么都没有发生

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

【问题讨论】:

    标签: google-maps bounds fitbounds setbounds


    【解决方案1】:

    如果我没记错的话,我假设您希望您的所有点都以尽可能高的缩放级别在地图上可见。我通过将地图的缩放级别初始化为 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);
    

    结果:成功!

    【讨论】:

      【解决方案2】:

      FitBounds就像你说的将边界扩展到第二张地图的边界之外,为了使边界完全一样,你可以设置中心和缩放。

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

      就像在这个 JSFiddle 中一样 - http://jsfiddle.net/KmNaX/

      【讨论】:

        【解决方案3】:

        嘿,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);
                    }
                });
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-03
          • 2014-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-25
          相关资源
          最近更新 更多