簡體   English   中英

在調用fitBounds后,google maps api v3 zoom_changed事件被觸發兩次

[英]google maps api v3 zoom_changed event fired twice after fitBounds called

我正在開發一個與谷歌地圖集成的網絡應用程序,我在調用fitBounds后遇到zoom_changed事件觸發兩次的問題。 我必須使用fitBounds的唯一引用是在updateMap中。 我附加事件監聽器的唯一時間是在createMap中。

對zoom更改處理程序的第二次調用導致另一個觸發器更新映射。

我已經讀過這篇文章: 在地圖上調用fitBounds函數時不要觸發'zoom_changed'事件

該標志適用於第一個事件。 但為什么還有另一個zoom_changed事件被觸發?

更好的是, 如何防止第二個zoom_changed事件被觸發?

這是我在控制台日志中看到的內容:

updateMap, fitbounds: 0
calling fitbounds...
zoom changed
mapEventHandler, fitbounds: 1
zoom changed
mapEventHandler, fitbounds: 0

這是我的更新映射函數的代碼,它調用fitBounds:

var mapZoomOrDragEventInProgress = false;
var fitBoundsCalledCount = 0;
var map = null;

updateMap = function () {
    console.log("updateMap, fitbounds: " + fitBoundsCalledCount);

    var bounds = fitBoundsForVenues();

    deleteAndUpdateMarkers();

    if (!mapZoomOrDragEventInProgress) {
        bn.spg.map.vars.fitBoundsCalledCount++;
        if (markers.length > 1) {
            console.log("calling fitbounds...");
            map.fitBounds(bounds);
        } else {
            console.log("setting zoom...");
            map.setCenter(bounds.getCenter());
            map.setZoom(13);
        }
    }

    mapZoomOrDragEventInProgress = false;
}

這是我的創建地圖功能:

createMap = function() {
    if (map === null) {
        var bounds = fitBoundsForVenues();

        var mapOptions = {
            center: bounds.getCenter(),
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        google.maps.event.addListener(map, 'zoom_changed', zoomChangedEventHandler)
        google.maps.event.addListener(map, 'dragend', dragendEventHandler);
     }
}

這是我的事件處理程序:

zoomChangedEventHandler = function () {
    console.log("zoom changed");
    console.log("mapEventHandler, fitbounds: " + fitBoundsCalledCount);

    //we want to handle only user zoom events, NOT zoom_events triggered from fit bounds or set_zoom
    if (fitBoundsCalledCount === 0) {
        mapZoomOrDragEventInProgress = true;
        var coords = getViewportCoordinates();
        updateVenuesAndMapAsync(coords.lat, coords.lng, coords.radius);
    } else {
        fitBoundsCalledCount--;
    }
}

我無法告訴你第二個zoom_changed被觸發的位置(我確定只有一次調用fitBounds()不是原因)

但是,而不是使用這些計數器我建議刪除zoom_ changed章24 -listener在開始的updateMap()並在年底重新分配監聽updateMap()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM