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 -
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google AJAX API" 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
-~----------~----~----~----~------~----~------~--~---