<div class="gmail_quote">On Wed, Feb 18, 2009 at 3:32 PM, Matt Kenny <span dir="ltr">&lt;<a href="mailto:matthewkenny@gmail.com">matthewkenny@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello Everyone,<br><br>I&#39;m having some trouble passing an array of properties to points created with the yahoo geocoder control: <a href="http://trac.openlayers.org/attachment/ticket/1784/yahoogeocoder.html" target="_blank">http://trac.openlayers.org/attachment/ticket/1784/yahoogeocoder.html</a><br>

<br>I&#39;ve got a series of address stored in a MySQL database, each of which also have a &#39;NAME&#39; property associated with an ambulance company. I&#39;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<br>

<br>The resulting points are geocoded in the correct location, but attributing these geocoded points with their respective &#39;NAME&#39; values is turning out to be an issue. Each geocoded point is being attributed with the &#39;NAME&#39; value for the last row in the array. I &#39;believe&#39; that the issue is related to scoping. I &#39;know&#39; that the issue is related to my knowledge of javascript, and any help would be greatly appreciated.<br>

<br>I&#39;ve got a live example running at: <a href="http://www.mkgeomatics.com/apps/air_ambulance/aab_embed_test.php" target="_blank">http://www.mkgeomatics.com/apps/air_ambulance/aab_embed_test.php</a><br>Clicking on the ambulance markers will yield the result which I have described above.<br>

<br>The geocoding function for the ambulance companies begins at line 333, geocodeGroundAmbulance().<br>The callback function begins at line 358, callbackGroundAmbulance().<br>Finally, the ID function begins at line 298, IdGroundAmbulance(feat).<br>

<br>Thanks in advance for any tips or suggestions.<br><font color="#888888"><br>-- <br>m a t t h e w k e n n y<br><a href="http://www.mkgeomatics.com" target="_blank">http://www.mkgeomatics.com</a><br>
</font><br>_______________________________________________<br>
Users mailing list<br>
<a href="mailto:Users@openlayers.org">Users@openlayers.org</a><br>
<a href="http://openlayers.org/mailman/listinfo/users" target="_blank">http://openlayers.org/mailman/listinfo/users</a><br>
<br></blockquote></div><br>Hi Matt,<br>
<br>
Yes, this looks like a scoping error to me.<br>
<br>
The problem is that within the <span class="sourceRowText">geocodeGroundAmbulance() function, the main loop that sets the </span><span class="sourceRowText"></span><span class="sourceRowText">groundAmbulance
variable finishes before the callback function to the response is
called.&nbsp; So when the function is called, the value of groundAmbulance
has been set to the last item in the array.<br>
<br>
One solution would be to build the callback within a separate function,
which would put the variable in a different scope.&nbsp; What I mean is
something like:<br>
<br>
var makeCallback = function(ga){<br>
&nbsp; return function(response){<br>
&nbsp;&nbsp;&nbsp; callbackGroundAmbulance(response, ga);<br>
&nbsp; }<br>
}<br>
<br>
</span><span class="sourceRowText">                               ygc.getLocation(query, makeCallback(</span><span class="sourceRowText">groundAmbulance))</span>
<div class="sourceRow"><span class="sourceRowText"><br>
There may be a more elegant solution, but I think this should do the trick.<br>
<br>
It&#39;s great to see somebody use that geocoder!&nbsp; It reminds me that I
should do the legwork to make it a proper addin.&nbsp; If you have any other
feedback on it, I&#39;d love to hear it.<br>
</span></div>
<div class="sourceRow"><span class="sourceRowText"><br>
</span></div>
<br>-- <br>Sebastian Benthall<br>OpenGeo - <a href="http://opengeo.org">http://opengeo.org</a><br>