开发者

ActionScript: How to push to multidimensional arrays and later retrieve just one 'row'

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 开发者_StackOverflow社区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 );                   


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 );         
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜