1.sprite类可以在AS工程中直接绘制在屏幕上.
2.要加到flex工程中需要包装,接口不匹配.
要显示Sprite可以使用UIComponent包装一下,例如
测试代码:
protected function button11_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var ui:UIComponent=new UIComponent();
pnl1.addElement(ui);
var fw:Sprite = new Sprite( );
var xian:Sprite = new Sprite( );
ui.addChild(fw);
fw.addChild(xian);
fw.graphics.beginFill(0xff0000,1.0);
fw.graphics.drawRect(0, 20, 20, 20);
fw.graphics.endFill();
//fw.addEventListener(MouseEvent.MOUSE_DOWN, ax);
//stage.addEventListener(MouseEvent.MOUSE_UP, sk);
}
具体用在openscale中,需要map1.map对象addChild(sprite对象)才行。
//事件处理器--面积
private var surface_bfirstpoint:Boolean; //是否开始测量,初始化图层和变量用。
//private var b_measureing:Boolean;
//private var surface_lineString:LineString=null; //线路对象,包含各节点
private var surface_PolygnFeature:PolygonFeature=null;
private var surface_drawContainer:Sprite = new Sprite(); //画线的容器
private var surface_firstpoint:Location=null;
private var surface_lastpoint:Location=null; //记忆要新增点之前的已加点位置,画线用。
private var surface_accuracies:HashMap=new HashMap();
private var surface_result:String;
private var surface_unit:String;
private function handler_measurepolygn(event:MouseEvent):void
{
if(opemode!=5) return;
if(event.type==MouseEvent.CLICK) //单击开始画线或继续画线
{
trace(opemode +"in measure-polygn click");
//var ui:UIComponent=new UIComponent();
//ui.addChild(surface_drawContainer);
//_map.addChild(ui);
//we determine the point where the user clicked
var pixel:Pixel = new Pixel(_map.mouseX,_map.mouseY );
var curpoint:Location=_map.getLocationFromMapPx(pixel);
var _point:org.openscales.geometry.Point = new org.openscales.geometry.Point(curpoint.lon,curpoint.lat);
//trace("in click");
if(surface_bfirstpoint) //开始测量,创建临时图层
{
_map.addChild(surface_drawContainer); //!!重要,否则看不到鼠标移动时画临时线。即sprite要加入地图中才行。
//map1.doubleClickEnabled=true;
//_map.doubleClickEnabled=true;
surface_drawContainer.doubleClickEnabled=true; //!!重要,必须开启container的dbclick,否则不激发dblclick事件。
surface_drawContainer.graphics.clear(); //清除上次画的内容
//清空测距画布的内容。
if(distance_drawContainer!=null) distance_drawContainer.graphics.clear();
//清空上次测量添加的实体。
if(measure_drawLayer!=null) measure_drawLayer.removeFeatures(measure_drawLayer.features);
//清空结果显示
txt_measure.text="";
//
surface_firstpoint=_map.getLocationFromMapPx(pixel);//记忆第1个点
surface_lastpoint = _map.getLocationFromMapPx(pixel); //记忆最后一个点
//
if(_map.getLayerByIdentifier("MeasureLayer")==null) //如果没有创建测量图层,创建之
{
measure_drawLayer=new VectorLayer("MeasureLayer");
measure_drawLayer.editable = true;
measure_drawLayer.displayInLayerManager = false;
measure_drawLayer.scaleX=1;
measure_drawLayer.scaleY=1;
//var _accuracies:HashMap=null;
//_accuracies = new HashMap();
//_accuracies.put(Unit.DEGREE,2);
//_accuracies.put(Unit.RADIAN,4);
//_accuracies.put(Unit.SEXAGESIMAL,2);
//_accuracies.put(Unit.MILE,3);
//_accuracies.put(Unit.FOOT,2);
//_accuracies.put(Unit.INCH,1);
//_accuracies.put(Unit.KILOMETER,3);
//_accuracies.put(Unit.METER,0);
measure_drawLayer.projection = _map.projection;
measure_drawLayer.minResolution = _map.minResolution;
measure_drawLayer.maxResolution = _map.maxResolution;
_map.addLayer(measure_drawLayer);
}
var lring:LinearRing=null;
var polygon:Polygon=null;
lring = new LinearRing(new <Number>[_point.x,_point.y]);
lring.projection = _map.projection;
polygon = new Polygon(new <Geometry>[lring]);
polygon.projection = _map.projection;
var name:String = "polygon." + measure_drawLayer.idPolygon.toString();
measure_drawLayer.idPolygon++;
surface_PolygnFeature=new PolygonFeature(polygon,null,null,true);
surface_PolygnFeature.name = name;
surface_PolygnFeature.style = Style.getDefaultSelectedPolygonStyle();
measure_drawLayer.addFeature(surface_PolygnFeature);
//锁定,不再进入本流程
surface_bfirstpoint = false;
}
else
{
surface_lastpoint = _map.getLocationFromMapPx(pixel); //记忆最后一个点
//add the point to the linearRing
lring=(surface_PolygnFeature.geometry as Polygon).componentByIndex(0) as LinearRing;
lring.addPoint(_point.x,_point.y);
//surface_lastpoint = point;
measure_drawLayer.redraw(true);
}
//激发事件,让在函数外部对feature进行测量。
surface_PolygnFeature.registerListeners();
_map.dispatchEvent(new FeatureEvent(FeatureEvent.FEATURE_DRAWING_END,surface_PolygnFeature));
}
if(event.type==MouseEvent.MOUSE_MOVE) //新增点之前画临时线
{
//position of the last point drawn
if(surface_firstpoint!=null)
{
//!!! 这里try catch非常重要,否则鼠标移动不画临时线!! 找了半天原因!!!
//不是因为try catch这里,是前面要_map.appChild(..),把container加入到_map中才行。
try
{
surface_drawContainer.graphics.clear();
//_drawContainer.graphics.beginFill(0x00ff00,0.5);
//trace("起始点:" + _map.getMapPxFromLocation(surface_firstpoint).x + "," + _map.getMapPxFromLocation(surface_firstpoint).y);
//trace("最后点:" + _map.getMapPxFromLocation(surface_lastpoint).x + "," + _map.getMapPxFromLocation(surface_lastpoint).y);
surface_drawContainer.graphics.lineStyle(2, Style.getDefaultSelectedColor());
surface_drawContainer.graphics.moveTo(_map.mouseX, _map.mouseY);
surface_drawContainer.graphics.lineTo(_map.getMapPxFromLocation(surface_firstpoint).x,_map.getMapPxFromLocation(surface_firstpoint).y);
surface_drawContainer.graphics.moveTo(_map.mouseX, _map.mouseY);
surface_drawContainer.graphics.lineTo(_map.getMapPxFromLocation(surface_lastpoint).x,_map.getMapPxFromLocation(surface_lastpoint).y);
surface_drawContainer.graphics.endFill();
}
catch(e:Error)
{
//trace(e.message);
}
}
}
if(event.type==MouseEvent.DOUBLE_CLICK) //新增点之前画临时线
{
trace(opemode +"in measure-polygn dbl click");
if(surface_PolygnFeature!=null)
{
//画的点不够,还不是一个面
//(measure_drawLayer as VectorLayer).removeFeature(this._firstPointFeature);
//Check if the polygon (in fact, the linearRing) contains at least 3 points (if not, it's not a polygon)
if((surface_PolygnFeature.polygon.componentByIndex(0) as LinearRing).componentsLength>2)
{
//Apply the "finished" style
surface_PolygnFeature.style = Style.getDefaultPolygonStyle();
surface_PolygnFeature.registerListeners();
//
_map.dispatchEvent(new FeatureEvent(FeatureEvent.FEATURE_DRAWING_END,surface_PolygnFeature));
}
else
{
measure_drawLayer.removeFeature(surface_PolygnFeature);
}
measure_drawLayer.redraw(true);
}
//the polygon is finished
surface_bfirstpoint = true;
surface_firstpoint=null; //防止下次测量激发mousemove时,连到上次的终点。
}
}
private function trunc(val:Number,unit:Number):String{
if(!unit){
unit=2;
}
return Util.truncate(val,unit);
}
//测距、侧面积,激发事件,显示结果。
_map.addEventListener(FeatureEvent.FEATURE_DRAWING_END,dispmeasure);
private function dispmeasure(event:FeatureEvent):void
{
//显示距离
if(opemode==4)
{
if(distance_LineFeature && (distance_LineFeature.geometry as MultiPoint).components.length>1)
{
tmpDist = (distance_LineFeature.geometry as LineString).length;
distance_accuracies.put(Unit.DEGREE,2);
distance_accuracies.put("gon",2);
distance_accuracies.put(Unit.MILE,3);
distance_accuracies.put(Unit.FOOT,2);
distance_accuracies.put(Unit.INCH,1);
tmpDist *= Unit.getInchesPerUnit(ProjProjection.getProjProjection(measure_drawLayer.projection).projParams.units);
var distance_displaySystem:String = Unit.KILOMETER;
var tmpDist:Number=0;
//trace("jasdfasdf:" + _displaySystem);
switch (distance_displaySystem.toLowerCase())
{
case Unit.METER:
tmpDist = (distance_LineFeature.geometry as LineString).geodesicLength;
//tmpDist/=Unit.getInchesPerUnit(Unit.METER);
trace(tmpDist + "," + "asdad");
distance_result= trunc(tmpDist,distance_accuracies.getValue(Unit.METER));
distance_unit = Unit.METER;
break;
case Unit.KILOMETER:
tmpDist = (distance_LineFeature.geometry as LineString).geodesicLength;
//tmpDist/=Unit.getInchesPerUnit(Unit.KILOMETER);
tmpDist /= 1000;
distance_result= trunc(tmpDist,distance_accuracies.getValue(Unit.KILOMETER));
distance_unit ="km";// Unit.KILOMETER;
break;
}
}
txt_measure.text=distance_result + distance_unit;
}
//显示面积
if(opemode==5)
{
var area:Number = 0;
if(surface_PolygnFeature && (surface_PolygnFeature.geometry as Polygon).componentsLength == 1
&& ((surface_PolygnFeature.geometry as Polygon).componentByIndex(0) as LinearRing).componentsLength>2)
{
var surface_units:String=ProjProjection.getProjProjection(measure_drawLayer.projection).projParams.units;
((surface_PolygnFeature.geometry as Polygon).componentByIndex(0) as LinearRing).units = surface_units
area = ((surface_PolygnFeature.geometry as Polygon).componentByIndex(0) as LinearRing).geodesicArea;
area = Math.abs(area);
//
var surface_diaplaySystem:String=Unit.KILOMETER;
switch (surface_diaplaySystem.toLowerCase())
{
case Unit.KILOMETER :
//area = this.getGeodesicArea();
area=area/1000000;
surface_unit ="km²";
surface_result= area.toFixed(2);
break;
case Unit.METER :
area = area;//this.getGeodesicArea();
surface_unit = "m²";
surface_result=area.toFixed(2);
break;
}
txt_measure.text=surface_result + surface_unit;
}
}
}