ArcGIS Flex API 中的 Flex 技术(三)--异步特征
作者:Flyingis
Flex天生具备异步调用的能力,使得Flex能够很好的适应webgis应用中的常用功能,在ArcGIS Flex API中,最典型的莫过于地理要素信息的动态显示,例如对于关注的地理要素,当鼠标移动上去后显示出该要素的基本信息,ArcGIS Flex API Demo的"Show InfoWindow on mouse hover"就是这样的例子。
Code
<![CDATA[
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.Graphic;
private const m_content : InfoWindowRollOverContent = new InfoWindowRollOverContent();
override protected function createChildren():void
{
super.createChildren();
map.infoWindow.content = m_content;
map.infoWindow.labelVisible = false;
map.infoWindow.closeButtonVisible = false;
}
private function rollOverHandler( event : MouseEvent ) : void
{
const graphic : Graphic = Graphic( event.target );
const mapPoint : MapPoint = MapPoint( graphic.geometry );
m_content.lat = mapPoint.y.toFixed( 3 );
m_content.lon = mapPoint.x.toFixed( 3 );
map.infoWindow.show(mapPoint);
}
private function rollOutHandler( event : MouseEvent ) : void
{
map.infoWindow.hide();
}
]]>
<![CDATA[
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.Graphic;
private const m_content : InfoWindowRollOverContent = new InfoWindowRollOverContent();
override protected function createChildren():void
{
super.createChildren();
map.infoWindow.content = m_content;
map.infoWindow.labelVisible = false;
map.infoWindow.closeButtonVisible = false;
}
private function rollOverHandler( event : MouseEvent ) : void
{
const graphic : Graphic = Graphic( event.target );
const mapPoint : MapPoint = MapPoint( graphic.geometry );
m_content.lat = mapPoint.y.toFixed( 3 );
m_content.lon = mapPoint.x.toFixed( 3 );
map.infoWindow.show(mapPoint);
}
private function rollOutHandler( event : MouseEvent ) : void
{
map.infoWindow.hide();
}
]]>
当鼠标移动到地理要素(点、线、面)上方时,会调用rollOverHandler方法,显示出该地理要素的x/y坐标,之后由rollOutHandler清除显示的结果,整个过程不会同步刷新所有地图。Flex异步调用的特性和单线程机制,使得我们经常会在Flex中使用回调函数的方法,如之前写过的事件监听:
Code
<?xml version="1.0"?>
<!-- events/SimpleEventHandler.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp();">
<mx:Script><![CDATA[
import mx.controls.Alert;
private function initApp():void {
b1.addEventListener(MouseEvent.CLICK, myEventHandler);
}
private function myEventHandler(event:Event):void {
Alert.show("An event occurred.");
}
]]></mx:Script>
<mx:Button id="b1" label="Click Me"/>
</mx:Application>
<?xml version="1.0"?>
<!-- events/SimpleEventHandler.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp();">
<mx:Script><![CDATA[
import mx.controls.Alert;
private function initApp():void {
b1.addEventListener(MouseEvent.CLICK, myEventHandler);
}
private function myEventHandler(event:Event):void {
Alert.show("An event occurred.");
}
]]></mx:Script>
<mx:Button id="b1" label="Click Me"/>
</mx:Application>
Flex中如果希望实现同步,可以考虑WebService Components,异步调用最初会让人想到是基于性能的一种设计,现在换成WebService似乎走向了另外一个极端,于是出现这样的设计:
"在通过RemoteObject进行数据交互的时候触发invoke事件,把画面给锁住,result的时候解锁。"
但这只是RemoteObject在数据交互上同步,在画面上的动作还是有异步存在的,对于地图交互来说,还是异步设计更贴近实际应用。
Flyingis @ China
email: dev.vip#gmail.com
blog: http://flyingis.cnblogs.com/