Hey Gareth -- You have the closure setup correctly. The problem, though, is the way you use your localsearch variable (sorry I should have seen this before). You have it set as a global variable and you loop through it 5 times setting the callback method to call. Each time you do this, you replace the callback method. There can only be _one_ callback method for a localsearch instance. So you have 5 queries sent out that are all going to call the same callback method... the last one that was set, aka an anonymous function with "4" passed in.
All you need to do to fix it is to create a new localsearch instance for each search and not use a single global localsearch instance. Let me know if this works for you! -Ben On Tue, Oct 21, 2008 at 10:52 AM, GarethE <[EMAIL PROTECTED]> wrote: > > Hi Ben, > > Many thanks once again for taking the time to look through it, and > extra thanks for the code. > > I've pasted it in (oh, and I also fixed up some of it according to > your suggestions for speed by using only the jsapi reference and > Google loaders - thanks again), but it's not working. > > Have a look at > http://www.gedwardsandassoc.com/customerwork/morne/test7-1.htm > > The searches are sent out in sequence. The same sequence is seen at > the top of the callback function, but inside the new "return > function(...)", the sequence is always 4. And the searches can again > come back in any order. > > My word - this shouldn't be so hard to do! > > Please let me know if you get it working - in the meantime I'm reading > anything and everything I can understand on closures and delegate-like > behaviour in javascript. > > There's a solution in there somewhere! > > G. > > On Oct 20, 7:05 pm, "Ben Lisbakken" <[EMAIL PROTECTED]> wrote: > > Gareth -- > > Try this:http://paste-it.net/public/c1c2881/ > > (don't forget to change your API keys back..) > > > > I changed your line here: > > localSearch.setSearchCompleteCallback(null, function() { > > SearchCallback(address); }); > > to this: > > localSearch.setSearchCompleteCallback(null, SearchCallback(address)); > > > > Doing it this way will call SearchCallback at the time you call this > > setSearchCompleteCallback, and it will return an anonymous function that > has > > "address" snapshotted in there. > > > > Also, if you have a bit more time, I'd recommend checking out your script > > includes and Google API call syntax. You have 3 script includes in there > > and you can reduce it to one include (the jsapi one) if you change your > > syntax slightly to use the Google loader. Script includes are the most > > common performance reducer in web applications, so it might be worth a > look. > > For more information, learn about the loader here: > http://code.google.com/apis/ajax/documentation/ > > And you can see what the different syntax looks like by playing with the > > checkbox at the top here: > http://code.google.com/apis/ajaxsearch/documentation/reference.html > > > > -Ben > > > > > > > > On Sat, Oct 18, 2008 at 2:53 AM, GarethE <[EMAIL PROTECTED]> > wrote: > > > > > Hey Ben - thanks a million for the reply and sorry for my late > > > response but alas no joy. > > > > > I've set uphttp:// > www.gedwardsandassoc.com/CustomerWork/Morne/test7-0.htm > > > which is the original one where each of the 5 callbacks prints out a 4 > > > (note how clicking on the "Route!" button enough times can yield very > > > different routes from the same original array of postcodes). > > > > > And I've done > > >http://www.gedwardsandassoc.com/CustomerWork/Morne/test7-1.htm > > > with what I think is what I was supposed to do and alas it doesn't > > > work :( > > > > > I've added some debug code and I can see that each of the 5 searches > > > gets set up, but for some reason anything inside the return function() > > > {...} doesn't seem to be called. > > > > > I hope we can crack it (it would be very useful to lots of peeps in > > > the UK!) > > > > > Many thanks once again, > > > > > Gareth > > > > > On Oct 17, 6:01 pm, "Ben Lisbakken" <[EMAIL PROTECTED]> wrote: > > > > Hey Gareth -- > > > > In the function that prints out 5 4's, add this to the top of the > > > function: > > > > return function() { > > > > and then ofcourse close it at the end of the method. Let me know if > you > > > get > > > > it. > > > > > > Ben > > > > > > On Wed, Oct 15, 2008 at 12:11 PM, GarethE <[EMAIL PROTECTED]> > > > wrote: > > > > > > > Thanks Ben - closures look like just what I need. (I especially > liked > > > > > your description of the snapshot of the variable value at the time > the > > > > > call was made). > > > > > > > I've looked at your example, but have unfortunely not been able to > > > > > adapt it for .setSearchCompleteCallback. > > > > > > > This is the code so far (ShowTrip is called from a button): > > > > > > > var map; > > > > > var localSearch = new GlocalSearch(); > > > > > var addressArray = new Array("NW3 7EB","WC1X 9ED", "N16 5HZ", "SW19 > > > > > 8PB","NW3 7EB"); > > > > > var pointsArray = []; > > > > > > > function mapLoad() { > > > > > if (GBrowserIsCompatible()) { > > > > > map = new GMap2(document.getElementById("map")); > > > > > map.addControl(new GLargeMapControl()); > > > > > map.addControl(new GMapTypeControl()); > > > > > map.setCenter(new GLatLng(51.529251, -0.126343), 10, > > > > > G_HYBRID_MAP); > > > > > map.enableScrollWheelZoom(); > > > > > } > > > > > } > > > > > > > function ShowTrip() { > > > > > for (address in addressArray) { > > > > > localSearch.setSearchCompleteCallback(this, SearchCallback, > > > > > [address]); > > > > > localSearch.execute(addressArray[address]); > > > > > } > > > > > } > > > > > > > function SearchCallback(addressnum) { > > > > > if (localSearch.results[0]) { > > > > > document.getElementById("debug").innerHTML += addressnum; // > > > > > at the moment this shows 5 4's > > > > > var resultLat = localSearch.results[0].lat; > > > > > var resultLng = localSearch.results[0].lng; > > > > > pointsArray.push(new GLatLng(resultLat, resultLng)); > > > > > // above ideally would be: pointsArray[addressnum] = > new... > > > > > if (pointsArray.length == addressArray.length) > > > > > { GetDirections(); } > > > > > // above ideally: if (addressnum == addressArray.length-1) > > > > > { GetDirections(); } > > > > > } > > > > > else { > > > > > alert(address); > > > > > } > > > > > > > } > > > > > > > function GetDirections() { > > > > > > > // Displayed Route > > > > > gdir = new GDirections(map); > > > > > gdir.loadFromWaypoints(pointsArray); > > > > > } > > > > > > > If you could show me how to adapt it for closures that would be > great! > > > > > > > Many thanks, > > > > > > > Gareth > > > > > > > On Oct 15, 3:22 am, "Ben Lisbakken" <[EMAIL PROTECTED]> wrote: > > > > > > Hey Gareth -- > > > > > > Yeah, code would be best! But it looks like you need the power > of > > > > > closures. > > > > > > I wrote a post on this awhile back that was pretty detailed, so > I'm > > > > > going > > > > > > to link you to that rather than re-type it. Closures are a bit > > > confusing > > > > > > when you first learn them, but hopefully my explanation helps: > > > > > > http://groups.google.com/group/Google-AJAX-Search-API/browse_thread/t. > > > .. > > > > > > > > Regards, > > > > > > Ben > > > > > > > > On Tue, Oct 14, 2008 at 8:34 AM, GarethE < > [EMAIL PROTECTED]> > > > > > wrote: > > > > > > > > > I'm using a GlocalSearch to find the exact lat and lng for an > array > > > of > > > > > > > addresses and then passing an array of GLatLng from each top > result > > > to > > > > > > > GDirections (from what I've read this is necessary here in the > UK). > > > > > > > > > I've been able to loop the array of address and have set up > > > > > > > setSearchCompleteCallbacks for each one with a push into an > array > > > of > > > > > > > GLatLng items created from the lat and lng of the top search > > > result. > > > > > > > > > The fun part is of course the callbacks can complete in any > order > > > so > > > > > > > the directions are often messed up. Is there a way to pass a > > > sequence > > > > > > > value from the loop to each callback so the GLatLng items can > be > > > put > > > > > > > in their array in the same order as the addresses they came > from? > > > i.e. > > > > > > > the GLatLng created from the top search result of the first > address > > > > > > > would always go into position 1, second address position 2, > etc. > > > > > > > > > You can probably tell I'm new to asyncronous programming! - If > > > there's > > > > > > > a simpler way to do the same thing I'm all ears. > > > > > > > > > Code happily provided on request and thanks in advance.- Hide > > > quoted > > > > > text - > > > > > > > > - Show quoted text -- Hide quoted text - > > > > > > - Show quoted text -- Hide quoted text - > > > > - Show quoted text - > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/Google-AJAX-Search-API?hl=en -~----------~----~----~----~------~----~------~--~---
