12

有没有办法防止zoom_change事件被触发,如果它发生是由于fitBounds()

我遇到了一个问题,当地图发生缩放更改时,我需要从客户端在服务器上进行搜索,但是每次调用fitBounds()它都会导致zoom_change触发,从而导致客户端在服务器上进行另一次搜索。我只对用户完成的 zoom_change 感兴趣,而不是以编程方式使用fitBounds.

4

6 回答 6

10

当您在程序中执行 fitBounds 时,设置一个全局标志。当 zoom_changed 事件触发时,如果设置了标志,则清除它,否则将您的请求发送到服务器。

于 2012-10-25T22:57:17.223 回答
5

经过许多令人沮丧的小时,这是我的解决方案:

  var tiles_listener = google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
    var zoom_listener = google.maps.event.addListener(map, 'zoom_changed', function() {
      reloadMarkers();
    });
  });

请注意,我要求addListenerOnce确保tilesloaded仅在第一个完成zoome_changed后添加一次侦听器。fitBounds

于 2015-12-01T04:30:21.557 回答
4

这是一个老问题,但它可能对其他人有用。我遇到了同样的问题,zoom_changed每次fitBounds在向地图添加多个标记时调用 () 时都会触发。我所做的是map_changed在地图完全加载后将监听器添加到事件中,如下所示:

google.maps.event.addListener(map, 'tilesloaded', function() {
    google.maps.event.addListener(map, 'zoom_changed', function() {
于 2013-04-26T14:06:54.423 回答
1

There is another way to do that by removing that that event. I think it would be much easier and cleaner than adding a global variable.

 vm.map.events.bounds_changed = function() {
   google.map.event.removeListener(zoom_changed);
   }

Another way (forgotten to add that):

     vm.map.events.bounds_changed = function() {
google.map.event.addEventListener(zoom_changed,function(mapper,eventName,args){});
           }
于 2017-09-18T06:39:24.067 回答
0

setZoom()在我的应用程序中,我可以通过、fitBounds()或以编程方式触发缩放更改事件setOptions()

MAN = {};
MAN.globalMap = google.maps.Map(document.getElementById('map'));

然后,我通过包装函数路由所有编程调用以设置缩放,这些函数false在继续设置缩放之前将标志翻转到。

MAN.savedZoom = MAN.globalMap.getZoom();
MAN.saveZoomFlag = true;
// we want it to always be true, unless zoom was
// changed programatically.

MAN.setZoom = function setZoom(zoomLevel) {
  console.log("won't save that I'm setting zoomLevel to " + zoomLevel);
  this.saveZoomFlag = false;
  MAN.globalMap.setZoom(zoomLevel);
};

MAN.fitBounds = function fitBounds(bounds) {
  console.log("setting bounds, won't save zoomlevel.");
  this.saveZoomFlag = false;
  MAN.globalMap.fitBounds(bounds);
};

MAN.setOptions = function setOptions(options) {
  console.log("setting options, won't save zoomlevel.");
  this.saveZoomFlag = false;
  MAN.globalMap.setOptions(options);
};

I then declare a listeners. At first I was declaring only the first, and was puzzled that it wasn't working:

  google.maps.event.addListener(
    MAN.globalMap,
    'zoom_changed',
    function zoom_changed_listener() {
      console.log(
        "zoom changed to " +
        MAN.globalMap.getZoom() + "; " +
        (MAN.saveZoomFlag ? "saving." : "not saving.")
      );
      if (MAN.saveZoomFlag) {
        console.trace("saving");
        MAN.savedZoom = MAN.globalMap.getZoom();
      }
      MAN.saveZoomFlag = true;
    }
  );

you may also find the idle event helpful, however, if you're just trying to avoid the initial set. See more about the maps events here: https://developers.google.com/maps/documentation/javascript/events

于 2017-02-14T06:22:17.643 回答
-3

我想这应该很简单。有一个变量说

var z = map.getZoom(); //scope this variable appropriately(somewhere before you call the fitbounds()

然后在 map.fitbounds() 之后立即执行

map.setZoom(z); //this automatically changes the zoom level back to the previous level
于 2012-10-26T03:55:18.833 回答