From 0f4d2d78d42020c10f094e879dc0baa5e5299fb5 Mon Sep 17 00:00:00 2001 From: Pierce Bala Date: Tue, 23 Jun 2020 14:36:32 -0700 Subject: [PATCH] Add listeners to close infowindow when another is opened --- app/javascript/packs/lib/maps.js.erb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/javascript/packs/lib/maps.js.erb b/app/javascript/packs/lib/maps.js.erb index 65c4b225..397d8512 100644 --- a/app/javascript/packs/lib/maps.js.erb +++ b/app/javascript/packs/lib/maps.js.erb @@ -8,6 +8,7 @@ const circleMarkerImage = "<%= helpers.image_path('marker-circle.png') %>"; const showMarkerImage = "<%= helpers.image_path('show-icon.png') %>"; let map; +let lastOpenedInfoWindow; function initMap(x, y, image, draggable, callback){ image = typeof image !== 'undefined' ? image : currentLocationImage; @@ -81,8 +82,20 @@ function placeMarker(lat, lng, content, number){ }); marker.addListener('click', function() { + closeLastOpenedInfoWindow(); infoWindow.open(map, marker); + lastOpenedInfoWindow = infoWindow; }); + + google.maps.event.addListener(map, 'click', function(event) { + infoWindow.close(); + }); +} + +function closeLastOpenedInfoWindow() { + if (lastOpenedInfoWindow) { + lastOpenedInfoWindow.close(); + } } function generateContent(data){