views:

265

answers:

2

I am reading a set of latitude & Longitude Coordinates that define a polygone area. They are keyed to an area ID and I retrieve them from a SQL database. So for example, Area ID 153 might have 20 coordinates and area ID 77 might have 11 coordinates. I wish to save these in a 2-D array indexed by the area ID, and where each coordinate pair is combined into one Google LatLng object. At a later point I wish to retrieve just one row i.e. the set of coordinates for one area, and send them to a function that accepts an array of coordinates and draws the polygon on a map. Here's what I have:

private var coordsFromSql:ArrayCollection = new ArrayCollection();

var polyArray:Array = new Array();

for each(var item:COORDINATES in coordsFromSql)
{
    // add coordinates to the array for each Area id                
    polyArray[item.AREA_ID].push( new LatLng(item.LATITUDE, item.LONGITUDE) );
}

So this is where the first problem ocurrs. I don't know how to add a variable number of new items to a 2-D array into a known index. i.e considering polyArray like a 2-D spreadsheet how do I for example add values to 'row' 77 i.e. polyArray[77] ? If I run the above code, I get runtime error #1010 'A term is undefined and has no properties'

The second part of the question is how do you extract one 'row' as a new array? Using the above example to call a drawPolygon function, can I do this?

var polyArraySlice:Array = polyArray[77].slice();                   
drawPolygon(color,  polyArraySlice );                   
A: 

It looks like your loading code is close, but not quite. In your for loop you're doing:

polyArray[item.AREA_ID].push(/*...*/)

but you never actually put anything in the array there.

So your load would probably be something like this:

var polyArray:Array = []

for each(var item:COORDINATES in coordsFromSql)
{
    // add coordinates to the array for each Area id                
    var id:Number = item.AREA_ID;
    if(polyArray[id] == null) { polyArray[id] = [] }
    polyArray[id].push( new LatLng(item.LATITUDE, item.LONGITUDE) );
}

Getting a copy of the one of the individual locations would work just like you had:

var polyArraySlice:Array = polyArray[77].slice();                   
drawPolygon(color,  polyArraySlice );         
Herms
Thanks guys. Worked perfect!Mark.
Mark Kalbskopf
A: 

Ok, firstly, if you WANT to add items to an Array, you must either define the array like so

var myArray:Array = new Array(20);

which would define an Array of 20 or you must push, unshift them onto the Array. If you do the first new Array(20) method then you can add date to the array directly like polyArray[77] = "whatever".

However, I would not use an Array in this case since the AREA_ID will be unique. Instead use an Object.

var polyObject:Object = {};

then you can do this

if (polyObject[id] == undefined) polyObject[id] = [];
polyObject[id].push(new LatLng(item.LATITUDE, item.LONGITUDE) );
sberry2A
i'm pretty sure you can add to an array wherever you want. From my understanding arrays in Flex are really just hashtables with a 'size' parameter that's based on the highest numerical value that was added.
Herms
Right you are. Don't know why I didn't remember that. Too many other languages running through my brain perhaps. Anyway, it would still be better to use an Object then so you don't need to allocate memory for the undefined index addresses (which I think it does)
sberry2A