Information button user close button

I have a google.maps.InfoWindow instance and it has a custom close button. This is triggered by the onClick event. However, if I click the close button, the info window closes, which is expected, but a new marker appears on the map in the place where the info window is used. It looks like onClick = "infoWindow.close ()" - placeReclameMarker (event.latLng); at the same time.

    var map;
    var infoWindow;
    function initialize() {
        var latlng = new google.maps.LatLng(47.030698, 28.850098);
        var myOptions = {
            zoom: 15,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(
            document.getElementById("map_canvas"), myOptions);

        google.maps.event.addListener(map, 'click', function (event) {
            placeReclameMarker(event.latLng);
        });

    }

    function placeReclameMarker(location) {

        var marker = new google.maps.Marker({
            position: location,
            draggable: true,
            map: map
        });
        google.maps.event.addListener(marker, 'rightclick', function () {
            infoWindow = new google.maps.InfoWindow({
                content: '<input type="button" onclick="infoWindow.close()">'
            });
            infoWindow.open(map, marker);
        });
    }
+3
source share
2 answers

I'm not sure if you can visit the infoWindow variable in the infoWindow internal object. try:

content: '<input type="button" onclick="parent.infoWindow.close()">'

or

content: '<input type="button" onclick="parent.parent.infoWindow.close()">'

or

content: '<input type="button" onclick="alert(infoWindow)">'

the infoWindow variable must not be undefined.

luck

+1
source

. . , InfowWindow, .

;

var map;
var marker;
var infoWindow;
function whateverFunction() {
    // some code here to create the marker and the infoWindow
    infoWindow.open(map, marker);
    infoWindow.setContent('<input type=button name=Close onClick="marker.setMap(null);">');
}

.

0

Source: https://habr.com/ru/post/1756186/


All Articles