简体   繁体   English

使用fitBounds()和Google Maps API V3后使用setZoom()

[英]Using setZoom() after using fitBounds() with Google Maps API V3

I'm using fitBounds() to set the zoom level on my map too include all the markers currently displayed. 我正在使用fitBounds()在我的地图上设置缩放级别,也包括当前显示的所有标记。 However, when I have only one marker visible, the zoom level is 100% (... which zoom level 20 I think...). 但是,当我只有一个标记可见时,缩放级别为100%(...我认为缩放级别为20 ......)。 However, I don't want it to be that far zoomed in so the user can adjust the position of the marker without having to zoom out. 但是,我不希望它被放大,因此用户可以调整标记的位置而不必缩小。

I have the following code: 我有以下代码:

var marker = this.map.createMarker(view.latlng, this.markerNumber);
this.map.bounds.extend(view.latlng);
this.map.map.setCenter(this.map.bounds.getCenter());
this.map.map.fitBounds(this.map.bounds);
if (this.markerNumber === 1) {
  this.map.map.setZoom(16);
}
this.markerNumber++;

where this.map.bounds was previously defined as: 其中this.map.bounds先前定义为:

this.map.bounds = new google.maps.LatLngBounds();

However, the problem I am having is that the line this.map.map.setZoom(16); 但是,我遇到的问题是该行this.map.map.setZoom(16); doesn't work if I use this.map.map.fitBounds(this.map.bounds); 如果我使用this.map.map.fitBounds(this.map.bounds); , however, I know that line of code is correct because when I comment out the fitBound() line, the setZoom() magically starts functioning. 但是,我知道这行代码是正确的,因为当我注释掉fitBound()行时,setZoom()会神奇地开始运行。

Any ideas how I resolve this? 我有什么想法解决这个问题? I'm thinking of setting a maxZoom level as an alternative if I can't get this working. 我正在考虑设置一个maxZoom级别作为替代,如果我不能让它工作。

Alright, I've figured it out. 好吧,我已经明白了。 Apparently, the fitbounds() happens asynchronously, so you have to wait for a bounds_changed event before setting zoom works. 显然,fitbounds()是异步发生的,因此在设置缩放工作之前必须等待bounds_changed事件。

map = this.map.map;

map.fitBounds(this.map.bounds);
zoomChangeBoundsListener = 
    google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
        if (this.getZoom()){
            this.setZoom(16);
        }
});
setTimeout(function(){google.maps.event.removeListener(zoomChangeBoundsListener)}, 2000);

Update : See @Nequin's answer using addListenerOnce for a better solution that doesn't require a timeout. 更新 :使用addListenerOnce查看@ Nequin的答案,以获得不需要超时的更好解决方案。

google.maps.event.addListenerOnce(yourMap, 'bounds_changed', function(event) {
  if (this.getZoom() > 15) {
    this.setZoom(15);
  }
});

This solution works better… instead of waiting on timeout to remove listener. 此解决方案效果更好...而不是等待超时删除侦听器。 Call this directly before using fitBounds (I believe calling after will work as well). 在使用fitBounds之前直接调用它(我相信调用后也会起作用)。

I found the additional zoom to be a little jarring. 我发现额外的变焦有点刺耳。 If you set the maxZoom option before calling fitBounds (and then unset it in the callback) you can avoid it: 如果在调用fitBounds之前设置maxZoom选项(然后在回调中取消设置),则可以避免它:

map.setOptions({
    maxZoom: 10
});

map.setCenter(new google.maps.LatLng(-89, -179)); // make sure it changes so the idle listener gets called back

map.fitBounds(bounds);

var listener = google.maps.event.addListenerOnce(map, "idle", function()
{
    map.setOptions({
        maxZoom: 999
    });
});

I have simple and dirty solution. 我有简单而肮脏的解决方案。
Use If else ... 使用如果否则......

var marker = this.map.createMarker(view.latlng, this.markerNumber);
this.map.bounds.extend(view.latlng);
this.map.map.setCenter(this.map.bounds.getCenter()); 
if (this.markerNumber === 1) {
  this.map.map.setZoom(16);
} else {
   this.map.map.fitBounds(this.map.bounds);
}       
this.markerNumber++;

I just added one line to the function addBounds(position) and it fixed it, as the following shows: 我刚刚在函数addBounds(position)添加了一行并修复了它,如下所示:

    addBounds: function(position) {
        this.get('bounds', new google.maps.LatLngBounds()).extend(this._latLng(position));
        this.get('map').fitBounds(this.get('bounds'));
        this.get('map').setZoom(16);//line added
        return this;
    },

I think the correct way to do is that you need to set zoom level after map.fitBounds(bounds) . 我认为正确的方法是你需要在map.fitBounds(bounds)之后设置缩放级别。 Just simply add map.setZoom(16) after map.fitBounds(bounds) . 只需在map.fitBounds(bounds)之后添加map.setZoom(16) map.fitBounds(bounds) It works fine for me. 这对我来说可以。

All the solutions with event listeners didn't work for me (this.getZoom() always is undefined in the callback and this.setZoom() has no effect). 所有带有事件监听器的解决方案对我都不起作用(this.getZoom()在回调中始终未定义,this.setZoom()无效)。

I came up with this solution which worked nicely: 我提出了这个解决方案很好用:

function set_zoom() {
    if(map.getZoom()) {map.setZoom(map.getZoom() - 1);}
    else {setTimeout(set_zoom, 5);}
}
setTimeout(set_zoom, 5);

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

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