[OpenLayers-Users] Scoping Issue With Yahoo Geocoder

Sebastian Benthall seb at opengeo.org
Thu Feb 19 11:53:31 EST 2009


On Wed, Feb 18, 2009 at 3:32 PM, Matt Kenny <matthewkenny at gmail.com> wrote:

> Hello Everyone,
>
> I'm having some trouble passing an array of properties to points created
> with the yahoo geocoder control:
> http://trac.openlayers.org/attachment/ticket/1784/yahoogeocoder.html
>
> I've got a series of address stored in a MySQL database, each of which also
> have a 'NAME' property associated with an ambulance company. I'm using a php
> script to create a javascript variable which extracts all of these values
> from the database. A simple looping function then runs each of them through
> the yahoo geocoder
>
> The resulting points are geocoded in the correct location, but attributing
> these geocoded points with their respective 'NAME' values is turning out to
> be an issue. Each geocoded point is being attributed with the 'NAME' value
> for the last row in the array. I 'believe' that the issue is related to
> scoping. I 'know' that the issue is related to my knowledge of javascript,
> and any help would be greatly appreciated.
>
> I've got a live example running at:
> http://www.mkgeomatics.com/apps/air_ambulance/aab_embed_test.php
> Clicking on the ambulance markers will yield the result which I have
> described above.
>
> The geocoding function for the ambulance companies begins at line 333,
> geocodeGroundAmbulance().
> The callback function begins at line 358, callbackGroundAmbulance().
> Finally, the ID function begins at line 298, IdGroundAmbulance(feat).
>
> Thanks in advance for any tips or suggestions.
>
> --
> m a t t h e w k e n n y
> http://www.mkgeomatics.com
>
> _______________________________________________
> Users mailing list
> Users at openlayers.org
> http://openlayers.org/mailman/listinfo/users
>
>
Hi Matt,

Yes, this looks like a scoping error to me.

The problem is that within the geocodeGroundAmbulance() function, the main
loop that sets the groundAmbulance variable finishes before the callback
function to the response is called.  So when the function is called, the
value of groundAmbulance has been set to the last item in the array.

One solution would be to build the callback within a separate function,
which would put the variable in a different scope.  What I mean is something
like:

var makeCallback = function(ga){
  return function(response){
    callbackGroundAmbulance(response, ga);
  }
}

 ygc.getLocation(query, makeCallback(groundAmbulance))
There may be a more elegant solution, but I think this should do the trick.

It's great to see somebody use that geocoder!  It reminds me that I should
do the legwork to make it a proper addin.  If you have any other feedback on
it, I'd love to hear it.


-- 
Sebastian Benthall
OpenGeo - http://opengeo.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20090219/11773d16/attachment.html


More information about the Users mailing list