Reputation: 134
I have an instance of google.maps.InfoWindow, and it has a custom closing button. Which is triggered by onClick event. 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); 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);
});
}
Upvotes: 3
Views: 5070
Reputation: 876
I was having the same problem. Found the solution. The thing is you should not only close the InfowWindow, but kill the marker also.
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.
Upvotes: 0
Reputation: 15723
i'm not sure you can visit infoWindow variable in infoWindow object inner. 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.
good luck
Upvotes: 1