业务gis 怎么让别的开发人员不需要懂gis就可以搞开发? (三)
上回我们说了怎么搭建一个简单的加载flash的方法,在flexviewer的gis开发中有很多加载顺序要注意,比如swf顺序,xml的顺序,要在地图做某些交互操作必须得等这些东西加载完毕才能调用,还有你在flex写widget的注册了javascript调用的方法,所以我们在flex端抛出一个事件,通过as的ExternalInterface.call 告诉javascript端图形加载完毕,这么我们就要定义事件通讯,老实说我不太喜欢js那个事件,自己写个数组去记录这个消息。
1 /** 2 * Created with JetBrains WebStorm. 3 * User: haibalai 4 * Date: 15-12-9 5 * Time: 下午3:40 6 * To change this template use File | Settings | File Templates. 7 */ 8 9 var EventBus =function (){ 10 this.handlers={}; 11 this.objs = {}; 12 } 13 14 EventBus.prototype={ 15 constructor:EventBus, 16 17 /** 18 * 添加事件 19 * @param type 20 * @param handler 21 */ 22 addEventListener:function(type,handler){ 23 if(typeof this.handlers[type]=='undefined'){ 24 this.handlers[type]=new Array(); 25 } 26 this.handlers[type].push(handler); 27 }, 28 29 /** 30 * 删除事件 31 * @param type 32 * @param handler 33 */ 34 removeEventListener:function(type,handler){ 35 if(this.handlers[type] instanceof Array){ 36 var handlers=this.handlers[type]; 37 for(var i=0,len=handlers.length;i<len;i++){ 38 if(handler[i]==handler){ 39 handlers.splice(i,1); 40 break; 41 } 42 } 43 } 44 }, 45 46 /** 47 * 抛事件 48 * @param event 49 */ 50 dispatchEvent:function(event){ 51 52 53 if(!event.target){ 54 event.target=this; 55 } 56 if(this.handlers[event.type] instanceof Array){ 57 var handlers=this.handlers[event.type]; 58 this.objs[event.type] = [event.obj]; 59 for(var i=0,len=handlers.length;i<len;i++){ 60 handlers[i](event); 61 } 62 } 63 } 64 }
我们把这个事件类初始化到MapControl里面,我在Parameter类加入了swfid的属性,防止一个页面加载两个地图造成混乱。
1 var MapControl = function () { 2 /** 3 * 地图初始化parameter 具体参考Parameter类 4 * @type {Parameter} 5 */ 6 this.parameter = new Parameter(); 7 /** 8 * 添加事件主体 具体参考EventBus类 9 * @type {EventBus} 10 */ 11 this.eventBus = new EventBus(); 12 13 this.initialise =function () { 14 15 16 swfobjhash[this.parameter.div] = this; 17 var swfVersionStr = "11.4.0"; 18 var xiSwfUrlStr = ""; 19 var flashvars = {}; 20 21 var params = {}; 22 params.wmode = "opaque"; 23 params.quality = "high"; 24 params.bgcolor = "#ffffff"; 25 params.allowscriptaccess = "always"; 26 params.allowfullscreen = "true"; 27 var attributes = {}; 28 attributes.id = this.parameter.div; 29 attributes.name = this.parameter.div; 30 attributes.align = "middle"; 31 swfobject.embedSWF( 32 "http://localhost/mymap/index.swf" + this.parameter.getUrlString() , this.parameter.div, 33 this.parameter.width, this.parameter.height, 34 swfVersionStr, xiSwfUrlStr, 35 flashvars, params, attributes); 36 swfobject.createCSS("#" + this.parameter.div, "display:block;text-align:left;"); 37 } 38 39 } 40 41 var swfobjhash = new Object(); 42 43 44 //flex图形组件加载完毕回调 swfid是区分嵌入swf的标识码 45 function emapComplete(swfid) { 46 swfobjhash[swfid].map.emap = document.getElementById(swfid); 47 48 swfobjhash[swfid].eventBus.dispatchEvent({type: "Initialized"}); 49 }
我们再定义自己事件名称
1 /** 2 * Created with JetBrains WebStorm. 3 * User: haibalai 4 * Date: 15-12-9 5 * Time: 下午3:40 6 * To change this template use File | Settings | File Templates. 7 */ 8 var MapControlEvent = { 9 /** 10 * 地图初始化事件 11 */ 12 Initialized : "Initialized", 13 14 }
在html页面就可以这样
<script type="text/javascript"> var a = new MapControl(); a.parameter.config = "config-checkview.xml"; a.parameter.div= "flashContent"; a.parameter.width = "800"; a.parameter.height = "800"; a.eventBus.addEventListener(MapControlEvent.Initialized,initHandle); a.initialise(); function initHandle() { alert("加载完毕") } </script>
标签:
gis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律