Google Maps API Tutorial© 2006, 2007, 2008, 2009 Mike Williams |
Translate this page: |
Onload function and external controlsIf you've been following this tutorial, adding external controls that affect the map is straight forward. If, however, you made an initial decision to put all the API Javascript code into an onload function you'll need to make some of your variables and function declarations global.Any variables and functions that are declared local to the onload function cannot be accessed from click or mouseover events that occur on html elements outside the map. Here's an example of using external controls to change the zoom level of the map. All the functions which are called from clicks and mouseovers on html elements need to be global. That just means that you need to place the function declarations outside the onload function, like this // This function zooms in or out function myzoom(a) { map.setZoom(map.getZoom() + a); } function onLoad() { ...Similarly, any variables which are used by those global functions need to be global. That just means making the initial declaration of the variables before any function declarations, and then populating those global variables in your onload function. In this particular example, the myzoom() function needs to use the "map" variable, and the myclick() function which handles the sidebar needs to access the gmarkers[ ] and htmls[ ] arrays. <script type="text/javascript"> var gmarkers = []; var htmls = []; var map; function myclick(i) { . . . Potential Pitfalls
|