Created
May 19, 2016 22:14
-
-
Save mhmoudsami/2c7fd39ddfc44514411144d72ca0dd25 to your computer and use it in GitHub Desktop.
toggle opened infobox google map api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create sample map | |
var map = new google.maps.Map(mapCanavas ,{ | |
zoom: 10, | |
center: LatLng, | |
disableDefaultUI:true, | |
zoomControl: true, | |
zoomControlOptions: { | |
position: google.maps.ControlPosition.TOP_LEFT | |
}, | |
scrollwheel: false, | |
}); | |
// current active window | |
// we will use this prop to keep track of active window | |
map.active_window = false; | |
// create sample marker | |
var marker = new google.maps.Marker({ | |
map: map, | |
position: new google.maps.LatLng(latitude, longitude);, | |
title: 'Marker', | |
}); | |
// create sample infoox | |
var infobox = new InfoBox({ | |
content: "info Box", | |
disableAutoPan: false, | |
// maxWidth: 150, | |
pixelOffset: new google.maps.Size(40, -150), | |
zIndex: null, | |
boxStyle: { | |
opacity: 1, | |
width: "300px" | |
}, | |
closeBoxMargin: "12px 4px 2px 2px", | |
infoBoxClearance: new google.maps.Size(10, 10) | |
}); | |
// open infobox On Marker click Event | |
google.maps.event.addListener(marker, 'click', function() { | |
// center the map to the marker position | |
map.setCenter(marker.getPosition()); | |
// if there is active window | |
// close it | |
if (map.active_window != false) { | |
map.active_window.infobox.close(map,map.active_window.marker); | |
} | |
// else open it | |
infobox.open(map,marker); | |
// then add the active window to this prop | |
map.active_window = {infobox: infobox , marker: marker}; | |
}); | |
// Close InfoWindow On Click Ouside The Marker | |
google.maps.event.addListener(map, 'click', function(event) { | |
// close the infobox | |
infobox.close(map,marker); | |
// clear opened_box property | |
map.active_window = false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.1 check if the value of the active_window != false (meaning there is already active window opened)
1.1 close it first
1.2 open the other window
1.3 set the value of the active_window to the current opened window
. reset the value of active_window prop to false after closing the window