简体   繁体   English

谷歌地图API V3方法fitBounds()

[英]Google maps API V3 method fitBounds()

I have this code that renders a map. 我有这个渲染地图的代码。

function initialize() {
    var myOptions = {
      center: new google.maps.LatLng(45.4555729, 9.169236),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP,

panControl: true,
mapTypeControl: false,
panControlOptions: {
            position: google.maps.ControlPosition.RIGHT_CENTER
        },
zoomControl: true,
zoomControlOptions: {
    style: google.maps.ZoomControlStyle.LARGE,
    position: google.maps.ControlPosition.RIGHT_CENTER
},
scaleControl: false,
streetViewControl: false,
streetViewControlOptions: {
    position: google.maps.ControlPosition.RIGHT_CENTER
            }
        };
    var map = new google.maps.Map(document.getElementById("mapCanvas"),
        myOptions);



var Item_1 = new google.maps.LatLng(45.5983128 ,8.9172776);

var myPlace = new google.maps.LatLng(45.4555729, 9.169236);

var marker = new google.maps.Marker({
    position: Item_1, 
    map: map});

var marker = new google.maps.Marker({
    position: myPlace, 
    map: map});

var bounds = new google.maps.LatLngBounds(myPlace, Item_1);
map.fitBounds(bounds);

    }

Even if the two points are separated from 25 km I get this result: 即使这两个点与25公里分开,我也得到了这个结果:

在此输入图像描述

While I would like to render a higher level zoom. 虽然我想渲染更高级别的缩放。

Like this 像这样

在此输入图像描述

I use the method fitBounds() 我用的方法是fitBounds()

    var bounds = new google.maps.LatLngBounds(myPlace, Item_1);
    map.fitBounds(bounds);

Thanks for your support 感谢您的支持

This happens because LatLngBounds() does not take two arbitrary points as parameters, but SW and NE points 发生这种情况是因为LatLngBounds()不会将两个任意点作为参数,而是SW和NE点

use the .extend() method on an empty bounds object 在空边界对象上使用.extend()方法

var bounds = new google.maps.LatLngBounds();
bounds.extend(myPlace);
bounds.extend(Item_1);
map.fitBounds(bounds);

Demo at http://jsfiddle.net/gaby/22qte/ 演示http://jsfiddle.net/gaby/22qte/

LatLngBounds must be defined with points in (south-west, north-east) order. 必须使用(西南,东北)顺序中的点定义LatLngBounds Your points are not in that order. 你的积分不是那个顺序。

The general fix, especially if you don't know the points will definitely be in that order, is to extend an empty bounds: 一般修复,特别是如果你不知道这些点肯定会按顺序,是扩展一个空边界:

var bounds = new google.maps.LatLngBounds();
bounds.extend(myPlace);
bounds.extend(Item_1);
map.fitBounds(bounds);

The API will sort out the bounds. API将整理边界。

I have the same problem that you describe although I'm building up my LatLngBounds as proposed by above. 我有同样的问题,你描述虽然我正在建立我的LatLngBounds,如上所述。 The problem is that things are async and calling map.fitBounds() at the wrong time may leave you with a result like in the Q. The best way I found is to place the call in an idle handler like this: 问题是事情是异步的,并且在错误的时间调用map.fitBounds()可能会给你留下像Q中那样的结果。我找到的最好的方法是将调用放在这样的空闲处理程序中:

google.maps.event.addListenerOnce(map, 'idle', function() {
    map.fitBounds(markerBounds);
});
 var map = new google.maps.Map(document.getElementById("map"),{
                mapTypeId: google.maps.MapTypeId.ROADMAP

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

for (i = 0; i < locations.length; i++){
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
            map: map
        });
bounds.extend(marker.position);
}
map.fitBounds(bounds);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM