简体   繁体   中英

How to listen to multiple DOM events in Google map at the same time?

I am showing a Google map in one of my websites. And when bounds are changes in the Google map, I do some calculations. I calculate the coordinate values & store them in four separate textboxes. I am able to do that.

Presently, I am doing this only on bounds_changed event of the map. I want to perform the same operation in the following events also:

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

How to do this? I am not much familiar with JavaScript. I don't even know whether such a thing is possible or not? What changes should I make to the source code.

I have included a fiddle . But it does not work. It's just to show the code.

I took 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;

            });
        }

There's no direct way to use multiple events. Move the common 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 an 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');
....

I don't think there is any native support for that. The closest thing 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...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM