Nick Mischler
Nick Mischler

Reputation: 185

Delaying execution of Javascript function relative to Google Maps / geoxml3 parser?

I'm working on a implementing a Google map on a website with our own tiles overlays and KML elements. I've been previously requested to create code so that, for instance, when the page is loaded from a specific URL, it would initialize with one of the tile overlays already enabled. Recently, I've been requested to do the same for the buildings which are outlined by KML elements so that, arriving at the page with a specific URL, it would automatically zoom, center, and display information on the building.

However, while starting with the tile overlays work, the building KML does not. After doing some testing, I've determined that when the code which checks the URL executes, the page is still loading the KML elements and thus do not exist for the code to compare to or use:

Code for evaluating URL (placed at the end of onLoad="initialize()")

function urlClick() {
    var currentURL = window.location.href; //Retrieve page URL
    var URLpiece = currentURL.slice(-6);   //pull the last 6 digits (for testing)
    if (URLpiece === "access") {           //If the resulting string is "access":
        access_click();                      //Display accessibility overlay
    } else if (URLpiece === "middle") {    //Else if the string is "middle":
        facetClick('Middle College');        //Click on building "Middle College"
    };
};

facetClick();

function facetClick(name) {  //Convert building name to building ID.
    for (var i = 0; i < active.placemarks.length; i++) {
        if (active.placemarks[i].name === name) {
            sideClick(i)     //Click building whose id matches "Middle College"
        };
    };
};

Firebug Console Error

active is null
for (var i = 0; i < active.placemarks.length; i++) {

active.placemarks is which KML elements are loaded on the page, and being null, means no KML has been loaded yet. In short, I have a mistiming and I can't seem to find a suitable place to place the URL code to execute after the KMl has loaded. As noted above, I placed it at the end of onLoad="initialize()", but it would appear that, instead of waiting for the KML to completely load earlier in the function, the remainder of the function is executed:

onLoad="initialize()"

information();   //Use the buttons variables inital state to set up description
buttons();       //and button state
button_hover(0); //and button description to neutral.

//Create and arrange the Google Map.

//Create basic tile overlays.

//Set up parser to work with KML elements.
myParser = new geoXML3.parser({ //Parser: Takes KML and converts to JS.
    map: map,                   //Applies parsed KML to the map
    singleInfoWindow: true,
    afterParse: useTheData      //Allows us to use the parsed KML in a function
});
myParser.parse(['/maps/kml/shapes.kml','/maps/kml/shapes_hidden.kml']);
google.maps.event.addListener(map, 'maptypeid_changed', function() {
    autoOverlay();
});

//Create other tile overlays to appear over KML elements.

urlClick();

I suspect one my issues lies in using the geoxml3 parser (http://code.google.com/p/geoxml3/) which converts our KML files to Javascript. While the page has completed loading all of the elements, the map on the page is still loading, including the KML elements. I have also tried placing urlClick() in the parser itself in various places which appear to execute after all the shapes have been parsed, but I've had no success there either.

While I've been intending to strip out the parser, I would like to know if there is any way of executing the "urlClick" after the parser has returned the KML shapes. Ideally, I don't want to use an arbitrary means of defining a time to wait, such as "wait 3 seconds, and go", as my various browsers all load the page at different times; rather, I'm looking for some way to say "when the parser is done, execute" or "when the Google map is completely loaded, execute" or perhaps even "hold until the parser is complete before advancing to urlClick".


Edit: Here are links to the map with the basic form of the issue found above. Since I've been developing the next update to the map on a test server, facetClick() is not part of this live version and I instead use its output function sideClick(); however the error is still the same in this arrangement:

active is null
google.maps.event.trigger(active.gpolygons[poly],'click');

Map: http://www.beloit.edu/maps/

Map w/Accessibility: http://www.beloit.edu/maps/?access

Map w/Building Click: http://www.beloit.edu/maps/?middle


EDIT: Spent most of my day working on rebuilding the functionality of the parser in Javascript and, low and behold, without the parser it works just fine. I figure that is obvious as I have to define each shape individually before the code, rather than waiting for it to be passed along by the parser. It would seem the answer is "if you want unique URLs, drop the parser". >_<

Upvotes: 0

Views: 1494

Answers (1)

Suvi Vignarajah
Suvi Vignarajah

Reputation: 5788

I've come across a similar problem when dealing with waiting for markers and infoWindows to load before executing a function. I found a solution here ( How can I check whether Google Maps is fully loaded? see @Veseliq's answer) that using the google maps event listener function for checking when the map is 'idle', does the trick. I assume this solution would work for KML layers as well. Essentially what you will have to do is include the following at the end of your initialize function:

google.maps.event.addListenerOnce(map, 'idle', function(){
    // do something only the first time the map is loaded
});

In the API reference ( https://developers.google.com/maps/documentation/javascript/reference ) it states that the 'idle' event "is fired when the map becomes idle after panning or zooming". However, it seems to hold true that it is also fires on initial page load after everything in the map_canvas has loaded. And by using the addListenerOnce call, you ensure that it is never executed again after the initial page load (meaning it won't fire after a zoom or a pan action).


Second option:
As I mentioned you can take the callback approach, I believe this will only call your urlClick function after completing the parsing. Here's how you should probably arrange your code to make it work:

function someFunction(callback){
    myParser.parse(['/maps/kml/shapes.kml','/maps/kml/shapes_hidden.kml']);
    callback();
}

and then in your initialize you will have:

someFunction(function(){
    urlClick();
});

You will have to make your map and myParser variables global.

Resources: This link had an excellent and detailed brief on how callback functions work in javascript, http://www.impressivewebs.com/callback-functions-javascript/

Upvotes: 1

Related Questions