html - Delaying execution of Javascript function relative to Google Maps / geoxml3 parser? -
i'm working on implementing google map on website our own tiles overlays , kml elements. i've been requested create code that, instance, when page loaded specific url, initialize 1 of tile overlays enabled. recently, i've been requested same buildings outlined kml elements that, arriving @ page specific url, automatically zoom, center, , display information on building.
however, while starting tile overlays work, building kml not. after doing testing, i've determined when code checks url executes, page still loading kml elements , not exist code compare or use:
code evaluating url (placed @ end of onload="initialize()")
function urlclick() { var currenturl = window.location.href; //retrieve page url var urlpiece = currenturl.slice(-6); //pull last 6 digits (for testing) if (urlpiece === "access") { //if resulting string "access": access_click(); //display accessibility overlay } else if (urlpiece === "middle") { //else if string "middle": facetclick('middle college'); //click on building "middle college" }; }; facetclick();
function facetclick(name) { //convert building name building id. (var = 0; < active.placemarks.length; i++) { if (active.placemarks[i].name === name) { sideclick(i) //click building id matches "middle college" }; }; }; firebug console error
active null (var = 0; < active.placemarks.length; i++) { active.placemarks kml elements loaded on page, , being null, means no kml has been loaded yet. in short, have mistiming , can't seem find suitable place place url code execute after kml has loaded. noted above, placed @ end of onload="initialize()", appear that, instead of waiting kml load earlier in function, remainder of function executed:
onload="initialize()"
information(); //use buttons variables inital state set description buttons(); //and button state button_hover(0); //and button description neutral. //create , arrange google map. //create basic tile overlays. //set parser work kml elements. myparser = new geoxml3.parser({ //parser: takes kml , converts js. map: map, //applies parsed kml map singleinfowindow: true, afterparse: usethedata //allows use parsed kml in 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 appear on kml elements. urlclick(); i suspect 1 issues lies in using geoxml3 parser (http://code.google.com/p/geoxml3/) converts our kml files javascript. while page has completed loading of elements, map on page still loading, including kml elements. have tried placing urlclick() in parser in various places appear execute after shapes have been parsed, i've had no success there either.
while i've been intending strip out parser, know if there way of executing "urlclick" after parser has returned kml shapes. ideally, don't want use arbitrary means of defining time wait, such "wait 3 seconds, , go", various browsers load page @ different times; rather, i'm looking way "when parser done, execute" or "when google map loaded, execute" or perhaps "hold until parser complete before advancing urlclick".
edit: here links map basic form of issue found above. since i've been developing next update map on test server, facetclick() not part of live version , instead use output function sideclick(); error still same in arrangement:
active 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 of day working on rebuilding functionality of parser in javascript and, low , behold, without parser works fine. figure obvious have define each shape individually before code, rather waiting passed along parser. seem answer "if want unique urls, drop parser". >_<
i've come across similar problem when dealing waiting markers , infowindows load before executing function. found solution here ( how can check whether google maps loaded? see @veseliq's answer) using google maps event listener function checking when map 'idle', trick. assume solution work kml layers well. have include following @ end of initialize function:
google.maps.event.addlisteneronce(map, 'idle', function(){ // first time map loaded }); in api reference ( https://developers.google.com/maps/documentation/javascript/reference ) states 'idle' event "is fired when map becomes idle after panning or zooming". however, seems hold true fires on initial page load after in map_canvas has loaded. , using addlisteneronce call, ensure never executed again after initial page load (meaning won't fire after zoom or pan action).
second option:
mentioned can take callback approach, believe call urlclick function after completing parsing. here's how should arrange code make work:
function somefunction(callback){ myparser.parse(['/maps/kml/shapes.kml','/maps/kml/shapes_hidden.kml']); callback(); } and in initialize have:
somefunction(function(){ urlclick(); }); you have make map , myparser variables global.
resources: link had excellent , detailed brief on how callback functions work in javascript, http://www.impressivewebs.com/callback-functions-javascript/
Comments
Post a Comment