侦听器检测用户而不是API发生的地图更改

时间:2013-03-07 16:59:31

标签: javascript google-maps google-maps-api-3

我需要检测用户在Google地图中所做的任何更改(放大,缩小,移动等),但是当我通过像map.setZoom(缩放)或地图这样的API更改地图时,我无需启动。哑剧(centerLocation)。

我现在有:

google.maps.event.addListener(map, 'idle', function ()
{
    var search = $("#Search").val();
    var categoryId = $("#Category_Id").val();

    if (search != "" && categoryId > 0)
        companyGetAllByCategoryOnMapMoveSubmit();
});

但它通过用户缩放和javascript map.setZoom(8)启动,我只想听用户事件。

感谢。

2 个答案:

答案 0 :(得分:1)

保留一个状态变量,该变量可以识别侦听器的调用方式。假设您调用API,则状态变量为true,否则为false。在您的侦听器中检查该状态变量,如果为true则不执行任何操作并将状态变量设置为false。我的意思是

var globalState = false;
google.maps.event.addListener(map, 'idle', function ()
{
    if(!globalState) {
        var search = $("#Search").val();
        var categoryId = $("#Category_Id").val();

        if (search != "" && categoryId > 0)
            companyGetAllByCategoryOnMapMoveSubmit();
    }
    globalState = false;
});

现在无论何时调用API,都将globalState设置为true

globalState = true;
map.setZoom(8) /* or any other API */

答案 1 :(得分:0)

我不知道API提供的可能性。

如果你必须处理多个事件(并且需要在API调用时跳过它们),另一个解决方案可能是在API调用之前禁用所有事件(从而删除它们)并在API调用完成后重新启用它们。

看看我的示例代码:

<!DOCTYPE html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
        html, body, #map_canvas {
            margin: 0;
            padding: 0;
            height: 100%;
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>

        function initialize() {

            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(51.518998, -0.148315),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

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

            var changeListener;

            function handleMapChange() {
                console.log("Handle map change done by user!");
            }

            function zoomOut(mouseEvent) {
                // disable all events that should not get fired
                disableUserEvents();
                // do API call
                map.setZoom(map.getZoom() - 1);
                // re-enable temporary disabled events when API operation finished
                google.maps.event.addListenerOnce(map, "idle", enableUserEvents);
            }

            function disableUserEvents() {
                console.log("disable User Events");
                google.maps.event.removeListener(changeListener);
                changeListener = undefined;
            }

            function enableUserEvents() {
                console.log("enable User Events");
                changeListener = google.maps.event.addListener(map, "idle", handleMapChange);
            }

            // enable user triggered events
            enableUserEvents();

            // the following rightclick listener will trigger an API call (zoomOut)
            google.maps.event.addListener(map, "rightclick", zoomOut);

        }

        google.maps.event.addDomListener(window, 'load', initialize);

    </script>
</head>
<body>
    <div id="map_canvas"></div>
</body>