How to listen to multiple DOM events on google map at the same time?

I am displaying a google map on one of my websites. And when the boundaries change on the google map, I do some calculations. I calculate the coordinate values ​​and store them in four separate text boxes. I can do it.

I am currently only doing this on a bounds_changed

map event . I want to perform the same operation on the following events:

  1. zoom_changed
  2. center_changed
  3. tilesloaded
  4. idle
  5. drag, pull
  6. dragend
  7. dragstart

How to do it? I am not very familiar with JavaScript. I don't even know if this is possible or not? What changes should I make to the source code.

I turned on the violin . But it doesn't work. This is just to show the code.

I used the help from this page .

HTML code:

<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 code:

  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;

            });
        }

      

+3


source to share


2 answers


There is no direct way to use multiple events. Move the general function to a new function:

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

      

add a new function to bind to the event:



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

      

then add your events:

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

      

+4


source


I don't think there is any support for this. The closest I can think of:



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...
}

      

0


source







All Articles