网站建议:179001057@qq.com

ArcGIS API for flex遇到的问题解决(1)--地图服务url传值问题

技术2022-05-12  4

昨天一直困扰在Combobox的两个控件绑定问题,只要将语言写作 <esri:ArcGISDynamicMapServiceLayer id="DynamicService" url="{mapURL.selectedItem.data}"  load="loadMapLayer(event)">就会出现warning: unable to bind to property '' on class 'Object' (class is not an IEventDispatcher)的错误提示,而且跟踪错误时,DynamicService中除了url传入了值,其他的属性都是null,而把url写为固定的某个mapservices的URL,就不会出现错误,而且其他属性也能根据地图服务属性将值传入,但是我想要的是通过选择Combobx控件的某个选项,来确定我的url地址,调试了一下午加早上两个小时,终于解决了这个问题,而且两个控件也能够绑定了。解决的方法是:1.首先要解决那个warning的问题,在google中查到了下面的网页内容These warnings are there because if you have some code that modifies the individual fields of selectedItem, the binding mechanism will not be able to detect them. A better way to write this would be to create separate variables for the two values:

<?xml version="1.0"?><!-- Simple example to demonstrate the ComboBox control. --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script><![CDATA[

[Bindable]public var cards: Array = [ {label:"Visa", data:1}, {label:"MasterCard", data:2}, {label:"American Express", data:3} ];

[Bindable]public var selectedItemLabel:String = "";

[Bindable]public var selectedItemData:String = "";

private function closeHandler(event:Event):void {selectedItemLabel=ComboBox(event.target).selectedItem.label;selectedItemData=ComboBox(event.target).selectedItem.data;} ]]></mx:Script>

<mx:Panel title="ComboBox Control Example" height="75%" width="75%" layout="horizontal"paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">

<mx:ComboBox dataProvider="{cards}" width="150" close="closeHandler(event);"/>

<mx:VBox width="250"><mx:Text width="200" color="blue" text="Select a type of credit card."/><mx:Label text="You selected: {selectedItemLabel}"/><mx:Label text="Data: {selectedItemData}"/></mx:VBox>

</mx:Panel> </mx:Application>

于是,我声明了一个Bindable,createHandler函数将Combobox获取的url地址传给selectItemData  [Bindable]            public var selectItemData:String="";

  private function createHandler(event:Event):void          {                    selectItemData= ComboBox(event.target).selectedItem.data;          }   然后,在comboBox中通过creationComplete和change事件都调用上面的那个函数,当然,这两个函数的作用是不一样的,creationComplete相当于load,ComboBox加载完成后只调用一次,而change是每当改变comboBox的选项时都会触发事件,这两个函数保证comboBox当前状态下selectItemData都是有值的。

 <mx:ComboBox id="mapURL" selectedIndex="0" horizontalCenter="290"  dataProvider="{arr}" creationComplete="createHandler(event)" change="createHandler(event)" y="10"/>

2 下面是 loadMapLayer函数,把它写在DynamicService中的load事件中,开始,我选用了creationComplete事件触发loadMapLayer函数,但是没有得到想要的结果,想必只能用load了。。。<esri:ArcGISDynamicMapServiceLayer id="DynamicService" url="{selectItemData}"  load="loadMapLayer(event)">

    </esri:ArcGISDynamicMapServiceLayer>      //获取地图图层名称和index到ComboBox上             private function loadMapLayer(event:Event):void             {                                           //selectItemData= ComboBox(event.target).selectedItem.data;                 //获取图层信息数组                 var layerInfos:Array;                 layerInfos=DynamicService.layerInfos;                 var layers:Array = new Array();                 //遍历图层信息数组然后把图层的名称和index值添加到新的数组中                for(var i:int=0;i<layerInfos.length;i++)                {                    layers.push({label:layerInfos[i].name ,data:i});                 }                //给ComboBox设定数据源                layerList.dataProvider=layers;            }

以上就是我解决问题的方法,虽然过程曲折,但是还是很有收获的。。。。哈哈


最新回复(0)