Last active
December 16, 2015 06:39
-
-
Save christian-bromann/5392762 to your computer and use it in GitHub Desktop.
enable keyboard accessibility of Google Maps in StreetView mode (APIv3) after page load
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
/** | |
* Google StreetView provides keyboard accessibility to navigate with the arrow keys. | |
* Unfortunately you have to click on the map manually beforehand to activate this. | |
* This script enables that automatically. | |
* | |
* @requires Google Maps API v3 | |
*/ | |
/** | |
* Couple of things to take note of here, simply using .focus() or triggering a click | |
* action on the #map div element will not do the trick because those actions take place | |
* before the map actually gets rendered on the DOM. So, the first thing to acknowledge | |
* is the use of the tilesloaded event the google maps library provides. | |
*/ | |
google.maps.event.addListenerOnce(map, 'idle', function(){ | |
/** | |
* The click on the map relocates the pitch of the current point of view (pov). To prevent | |
* this, we have to add a click event listener which ignores our first click, to enable | |
* the keyboard accessibility | |
*/ | |
document.querySelector('svg').addEventListener('click', function(e) { | |
if(e.enableKeyAccess) { | |
e.preventDefault(); | |
} | |
}); | |
/** | |
* The second thing I came to find out is you can't just add $('#map').click() inside the event listener. | |
* This is because though the #map is the container div - the google maps script actually renders a whole | |
* bunch of other divs on top of it (which have higher z-indices and are also what actually holds your map tiles). | |
* After a bit trying I found a SVG element inside the map, which enables the keyboard accessibility. | |
*/ | |
var evt = document.createEvent("MouseEvents"); | |
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, true, true, true, 0, null); | |
evt.enableKeyAccess = true; | |
document.querySelector('svg').dispatchEvent(evt); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment