简体   繁体   English

信息窗口自定义关闭按钮

[英]info window custom close button

I have an instance of google.maps.InfoWindow, and it has a custom closing button. 我有一个google.maps.InfoWindow的实例,它有一个自定义关闭按钮。 Which is triggered by onClick event. 这是由onClick事件触发的。 However, if I click on close button, the info window is closed, which is expected, but a new marker appears on the map, on a place where info window use to be. 但是,如果我单击关闭按钮,信息窗口将关闭,这是预期的,但地图上会出现一个新标记,位于信息窗口所用的位置。 It looks like the onClick="infoWindow.close()" is the placeReclameMarker(event.latLng); 看起来像onClick =“infoWindow.close()”是placeReclameMarker(event.latLng); simultaneously. 同时。

    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);
        });
    }

i'm not sure you can visit infoWindow variable in infoWindow object inner. 我不确定你可以访问infoWindow对象内部的infoWindow变量。 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)">'

infoWindow variable must not to be undefined. infoWindow变量不能是未定义的。

good luck 祝好运

I was having the same problem. 我遇到了同样的问题。 Found the solution. 找到了解决方案。 The thing is you should not only close the InfowWindow, but kill the marker also. 问题是你不仅要关闭InfowWindow,还要杀死标记。

Try this; 尝试这个;

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);">');
}

Worked perfectly for me. 为我工作完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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