I am trying to create a county map of Illinois using x,y coordinates from a shp file开发者_JAVA百科. I have the x,y coordinates and the county names saved in a CSV file that I have no problem reading in via ActionScript 3 (and the map looks great), but in order to save time in my future application I would like to save each county shape as a permanent shape in my application and give them labels, i.e. Sprite1 (label='Champaign'). Is this possible? If so, any help would be greatly appreciated.
In case this is not possible I am trying an alternate solution: I create a new sprite (var spr:Sprite = new Sprite();) for each county, draw it using spr.graphics.drawPath, and give it a name (spr.name='Champaign') and then 'push' it to a vector of sprites (var xy_sprites:Vector. = new Vector.();). This would be great, but it doesn't work when I try to loop through each sprite in the vector and add an EventListener to that Sprite to pop up the name when you MouseOver any of the counties. Is the Sprite data type not the correct way to go about this or am a missing something about Sprites?
Some of my code to draw the shapes and save in a vector:
function drawXYMap(str:String):Vector.<Sprite> {
var arr:Array = str.split("\n");
var xy_Sprites:Vector.<Sprite> = new Vector.<Sprite>();
for (var i:int=0; i<arr.length-1; ++i) {
var spr:Sprite = new Sprite();
spr.graphics.lineStyle(1.0, 0x000000);
spr.graphics.beginFill(0x666699);
arr[i] = arr[i].split(',');
var xy_commands:Vector.<int> = new Vector.<int>();
var xy_coord:Vector.<Number> = new Vector.<Number>();
for (var j:int=1; j<arr[i].length; ++j) {
xy_coord.push(arr[i][j]*6);
if (j==1) {
xy_commands.push(1); // 1 is a move-to command
var cntry:String = arr[i][j-1] as String; //country name
}
else if (j % 2 == 1) {
xy_commands.push(2); // 2 is a line-to command
}
}
spr.graphics.drawPath(xy_commands, xy_coord);
spr.name = cntry;
xy_Sprites.push(spr);
addChild(spr);
}
return xy_Sprites;
}
But I can't seem to add an Event Listener to each sprite in the vector of sprites I created:
var str:String = csvLoader.data as String;
var xy_spr:Vector.<Sprite> = drawXYMap(str);
for each (var spr:Sprite in xy_spr) {
spr.addEventListener(MouseEvent.MOUSE_OVER,onOver);
}
function onOver(e:MouseEvent):void {
spr.alpha = .25;
}
Any help would be great. Thanks!
You could put all of your x, y co-ords into a Vector data structure, and then use the graphics.drawPath() method, to iterate over the co-ords any time you instantiate the county.
If you created a class that extended Shape, you could add a Vector that references the CSV file, and every time you create the shape, it automatically draws the path.
Check out this useful document from Adobe:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WSA8BD9022-BAB1-46d3-9B26-0D9649743C8E.html
PS don't forget to use the proper drawing commands for the drawPath(). You should be able to interact with it as normal if you do.
精彩评论