<div class="gmail_quote">On Wed, Feb 18, 2009 at 3:32 PM, Matt Kenny <span dir="ltr"><<a href="mailto:matthewkenny@gmail.com">matthewkenny@gmail.com</a>></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'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'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<br>
<br>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.<br>
<br>I'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. 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. What I mean is
something like:<br>
<br>
var makeCallback = function(ga){<br>
return function(response){<br>
callbackGroundAmbulance(response, ga);<br>
}<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'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.<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>