Zoom repeats multiple google maps

Asked

Viewed 67 times

1

I have a infowindow on my map. Every time I give a dragstart I close this infowindow. My problem is I give one infowindow.open(map, marker);, to open the infowindow. Every time I do that, he adds another infowindow in DOM. So when I make the request map.setZoom(map.getZoom() + 1);, it adds several zoom, according to the times I called the infowindow.

Follows the code:

HTML

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA-OlPHUh2W7LUZXVjQjjxQ8fdCWdJISmc"></script>

CSS

#map{width:500px; height:500px; float:left;}

JAVASCRIPT

var map;
var uluru = {lat:-17,lng: -47};
var infowindow = null;
function initialize(){
        var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 10,
                    center: uluru,
                    mapTypeId: google.maps.MapTypeId.SATELLITE
        });

    var infowindow = new google.maps.InfoWindow({
                    content: '<p><a href="#" id="zoom_mais"><b>+++++</b> </a> | <a href="#" id="zoom_menos"><b>-------</b></a></p>'
        });

    var marker = new google.maps.Marker({
                    position: uluru,
                    draggable: true,
                    map: map,
        });

    marker.addListener('click', function() {
                    infowindow.open(map, marker);
        });

        google.maps.event.addListener(marker, 'dragstart', function() {
                    if (infowindow) {
                        infowindow.close();
                        //infoWindow = null;
                    }
                    infowindow;
                    console.log('dragstart')
        });


    google.maps.event.addListener(marker, 'dragend', function () {
                    map.setCenter(this.getPosition()); // Set map center to marker position
          infowindow.open(map, marker);

        }); 

    //ZOOM
                google.maps.event.addListener(infowindow, 'domready', function() {
                    document.getElementById('zoom_mais').addEventListener('click', function(e) {
                        map.setZoom(map.getZoom() + 1);
                        console.log(map.getZoom());
                    });
                    document.getElementById('zoom_menos').addEventListener('click', function(e) {
                        map.setZoom(map.getZoom() - 1);
                        console.log(map.getZoom());         
                    });
                });

}

initialize();

This example running here.

Just move the Marker a 4 times and click the zoom. You will see that it increased 4 times the zoom.

  • What exactly is your doubt?

  • 3

    @abcd so that the code does not get lost in the comments, always add in the question, even if it is linked, so the question is complete by itself. :)

1 answer

0

I was able to solve the problem by removing this option:

google.maps.event.addListener(infowindow, 'domready', function() {});

In fact, I adapted it to my situation, because every time I called it with an event, another was added.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.