如何同时在谷歌地图中收听多个DOM事件?

时间:2014-10-07 05:43:47

标签: javascript jquery google-maps dom javascript-events

我在其中一个网站上展示了Google地图。当Google地图中的边界发生变化时,我会进行一些计算。我计算坐标值&将它们存储在四个单独的文本框中。我能够做到这一点。

目前,我只在地图的bounds_changed事件中执行此操作。我想在以下事件中执行相同的操作:

1) zoom_changed
2) center_changed
3) tilesloaded
4) idle
5) drag
6) dragend
7) dragstart

怎么做?我对JavaScript不太熟悉。我甚至不知道这样的事情是否可能?我应该对源代码做出哪些更改。

我添加了fiddle。但它不起作用。这只是为了显示代码。

我从this page获得了帮助。

HTML代码:

<div id="googleMap" style="width:900px;height:500px;">

</div>   

<input type="text" id="north" name="north">
<input type="text" id="east" name="east">
<input type="text" id="west" name="west">
<input type="text" id="south" name="south">

JavaScript代码:

  function loadScript()
    {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false&callback=initialize";
        document.body.appendChild(script);
    }
    window.onload = loadScript; 

function initialize()
        {
            var myLatlng=new google.maps.LatLng(-25.363882, 131.044922);
            var mapProp =
            {
                center: new google.maps.LatLng(-25.363882, 131.044922),
                zoom:6,
                maxZoom: 8,
                minZoom:2,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
            var image = 'mapmarkers/you-are-here-2.png';
            var marker = new google.maps.Marker
            ({
                position: myLatlng,
                map: map,
                title: 'I am Here.',
                icon: image,
                tooltip: '<B>This is a customized tooltip</B>'
            });

            google.maps.event.addListener(map, 'bounds_changed', function ()
            {           
                var bounds = map.getBounds();
                var southWest = bounds.getSouthWest();
                var northEast = bounds.getNorthEast();
                var getcentre=bounds.getCenter();
                var ne = map.getBounds().getNorthEast();
                var sw = map.getBounds().getSouthWest();

                var centre_lat=getcentre.lat();
                var centre_long=getcentre.lng();
                var myLatlng=new google.maps.LatLng(centre_lat,centre_long);
                var mapProp =
                {
                    center: new google.maps.LatLng(centre_lat,centre_long),
                    zoom:6,
                    maxZoom: 8,
                    minZoom:2,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                var north2=ne.lat();
                var east2=ne.lng();
                var south2=sw.lat();
                var west2=sw.lng();

                document.getElementById("north").value = north2;
                document.getElementById("east").value = east2;
                document.getElementById("west").value = west2;
                document.getElementById("south").value = south2;

            });
        }

2 个答案:

答案 0 :(得分:4)

没有直接的方法来使用多个事件。将公共函数移动到新函数:

function common(){
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    ....
}

添加一个新函数来绑定事件:

function bind(eventName){
    google.maps.event.addListener(map, eventName, function (){
         common();
    });
}

然后添加您的活动:

bind('zoom_changed');
bind('center_changed');
bind('tilesloaded');
bind('idle');
....

答案 1 :(得分:0)

我认为没有任何原生支持。我能想到的最接近的事情是:

function addEventListeners(map, target, events, fn) {

    for (var i=0; i < events.length; i++)
        target.addListener(map, events[i], fn);
}

addEventListeners(map, 
                  google.maps.event, 
                  ['zoom_changed', 'bounds_changed'], // list your events
                  theFunc); // function to execute

function theFunc(){
    var bounds = map.getBounds(); // etc...
}