Hi Ranglo -
The issue with your code was that the list[i] variable is actually not
equal to the [i] you think it is - it's equal to the final [i] in the
loop - because of the way variable scope works in JavaScript. The
solution is to move that bit out of the loop into its own function.
For example:
private function onResult(e:ResultEvent):void
{
var list:ArrayCollection = e.result.map.loc;
var arrayObject:Array = new Array();
for(var i:int=0; i<list.length; i++)
{
arrayObject[i] = new
LatLng(list[i].lat, list[i].lon);
}
createPolygon(arrayObject, list[i].name);
}
function createPolygon(arrayObject, name) {
p = new Polygon(arrayObject);
p.addEventListener(MapMouseEvent.CLICK, function
(e:MapMouseEvent):void {
map.openInfoWindow(, new InfoWindowOptions({content: name}));
//This is the bit I suspect is completely wrong
}
- pamela
On Tue, Apr 28, 2009 at 9:33 PM, Ranglo <[email protected]> wrote:
>
> Thanks Pamela,
>
> Could you go in to a bit more detail, my skills with arrays are
> somewhat limited?
>
> Thanks
>
> On Apr 20, 12:26 pm, pamela fox <[email protected]> wrote:
>> Hi Ranglo-
>>
>> The problem you're running into is that of function closure. The [i]
>> referenced in the event listener callback will always be the last
>> value of the i, not the value at which it was assigned. You should
>> wrap it in a function instead. There's an example
>> here:http://code.google.com/apis/maps/documentation/flash/events.html#Even...
>>
>> - pamela
>>
>>
>>
>> On Sun, Apr 19, 2009 at 6:53 PM, Ranglo <[email protected]> wrote:
>>
>> > I am currently trying to learn FLEX to improve my web GIS projects,
>> > however I have run into a problem. So far I have successfully managed
>> > to create polygon data from a SQL database, now I am trying to add
>> > infowindow functionality. I have followed the state map demo but am
>> > unsure how to populate the infowindow with the name field from my
>> > database. The function I have tried so far is below. Any help is much
>> > appreciated.
>>
>> > private var p:Polygon;
>>
>> > private function onResult(e:ResultEvent):void
>> > {
>> > var list:ArrayCollection = e.result.map.loc;
>> > var arrayObject:Array = new Array();
>> > for(var i:int=0; i<list.length; i++)
>> > {
>> > arrayObject[i] = new LatLng(list[i].lat,
>> > list[i].lon);
>> > }
>> > p = new Polygon(arrayObject);
>> > p.addEventListener(MapMouseEvent.CLICK,
>> > function
>> > (e:MapMouseEvent):void
>> > {
>> > map.openInfoWindow(event.latLon,
>> > new InfoWindowOptions({content:
>> > list[i].name})); //This is the bit I suspect is completely wrong
>> > }
>> > }- Hide quoted text -
>>
>> - Show quoted text -
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API For Flash" 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-maps-api-for-flash?hl=en
-~----------~----~----~----~------~----~------~--~---