转自:http://www.cnblogs.com/zitsing/archive/2012/03/02/2377083.html

前言

Web Gis顾名思义就是通过浏览器方式操作的地理系统。通过浏览器方式可以采用很多技术,主流的有 Html+Javascript、Flex、Silverlight。像大的公司谷歌百度既有技术又有人的,都是采用Html+Javascript.这样可以节约流量,提高载入速度,增加用户体验。而一般的企业级应用则采用Flex或Silverlight的居多。而我也属于小企业级的应用,没有很多用户访问,大多在局域网,面对的客户也都是傻瓜级的,使用的浏览球也都是IE6,所以我这个应用开发框架就是采用Flex,采用这个好处就是不用处理多浏览器兼容问题了。

开发框架

  • 开发工具:Adobe® Flash® Builder™ 4
  • 开发包:ArcGIS API for Flex 2.5
  • 地图服务: arcgis、谷歌地图、geoserver

这个框架是经过实践得出的,特别是ArcGIS API for Flex这个包,以前也使用过其他的开源的包,谷歌地图也有 for flex 的包。但相比这个,这个功能齐全,文档丰富。开发一个GIS方面的应用是绝对没问题的。

搭建Demo

  1. 打开Flash® Builder™ 4 新建一个名为FlexWebGis Web工成,把agslib-2.5-2011-11-30.swc包复制到lib下。
  2. 增加一个flex.web.gis.layer包,下面建一个GoogleMapLayer.as类,代码如下:
复制代码
 1 package flex.web.gis.layer
 2 {
 3     import com.esri.ags.SpatialReference;
 4     import com.esri.ags.geometry.Extent;
 5     import com.esri.ags.geometry.MapPoint;
 6     import com.esri.ags.layers.TiledMapServiceLayer;
 7     import com.esri.ags.layers.supportClasses.LOD;
 8     import com.esri.ags.layers.supportClasses.TileInfo;
 9 
10     import flash.net.URLRequest;
11 
12     //扩展TiledMapServiceLayer图层实现加载google地图13     public class GoogleMapLayer extends TiledMapServiceLayer
14     {
15         private var _tileInfo:TileInfo=new TileInfo();
16         public var _baseURL:String="t@128";
17 
18         public function GoogleMapLayer()
19         {
20             super();
21             buildTileInfo();
22             setLoaded(true);
23         }
24 
25         override public function get fullExtent():Extent
26         {
27             return new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787, new SpatialReference(102113));
28         }
29 
30         override public function get initialExtent():Extent
31         {
32             return new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787, new SpatialReference(102113));
33         }
34 
35         override public function get spatialReference():SpatialReference
36         {
37             return new SpatialReference(102113);
38         }
39 
40         override public function get tileInfo():TileInfo
41         {
42             return _tileInfo;
43         }
44 
45         //获取矢量地图46         override protected function getTileURL(level:Number, row:Number, col:Number):URLRequest
47         {
48             var url:String="http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + "&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil";
49             if (_baseURL == "s@92")
50             {
51                 url="http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + "&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil";
52             }
53             if (_baseURL == "t@128")
54             {
55                 url="http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + ",r@169000000&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil";
56             }
57             if (_baseURL == "m@161000000")
58             {
59                 url="http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + "&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil";
60             }
61             return new URLRequest(url);
62         }
63 
64         private function buildTileInfo():void
65         {
66             _tileInfo.height=256;
67             _tileInfo.width=256;
68             _tileInfo.origin=new MapPoint(-20037508.342787, 20037508.342787);
69             _tileInfo.spatialReference=new SpatialReference(102113);
70             //_tileInfo.lods=[new LOD(5, 4891.96981024998, 18489297.737236), new LOD(6, 2445.98490512499, 9244648.868618), new LOD(7, 1222.99245256249, 4622324.434309), new LOD(8, 611.49622628138, 2311162.217155), new LOD(9, 305.748113140558, 1155581.108577), new LOD(10, 152.874056570411, 577790.554289), new LOD(11, 76.4370282850732, 288895.277144), new LOD(12, 38.2185141425366, 144447.638572), new LOD(13, 19.1092570712683, 72223.819286), new LOD(14, 9.55462853563415, 36111.909643), new LOD(15, 4.77731426794937, 18055.954822)];71             _tileInfo.lods=[new LOD(9, 305.748113140558, 1155581.108577),new LOD(10, 152.874056570411, 577790.554289), new LOD(11, 76.4370282850732, 288895.277144), new LOD(12, 38.2185141425366, 144447.638572), new LOD(13, 19.1092570712683, 72223.819286), new LOD(14, 9.55462853563415, 36111.909643), new LOD(15, 4.77731426794937, 18055.954822),new LOD(16, 2.38865713397468, 9027.977411),new LOD(17, 1.19432856685505, 4513.988705),new LOD(18, 0.597164283559817, 2256.994353),new LOD(19, 0.298582141647617, 1128.497176)];
72         }
73     }
74 }
复制代码

     3.编辑FlexWebGis.mxml文件,代码如下:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>  2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   3                xmlns:s="library://ns.adobe.com/flex/spark"   4                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:esri="http://www.esri.com/2008/ags" xmlns:layer="flex.web.gis.layer.*" creationComplete="application1_creationCompleteHandler(event)">  5     <fx:Script>  6         <![CDATA[  7             import com.esri.ags.events.MapEvent;  8             import com.esri.ags.utils.WebMercatorUtil;  9             import com.esri.ags.geometry.MapPoint; 10             import mx.events.FlexEvent; 11  12             protected function application1_creationCompleteHandler(event:FlexEvent):void 13             { 14                 esriMap.addLayer(googleMapLayer); 15             } 16  17  18             protected function esriMap_loadHandler(event:MapEvent):void 19             { 20                 //设置地图中心点21                 esriMap.centerAt(WebMercatorUtil.geographicToWebMercator(new MapPoint(118.610037,31.138343)) as MapPoint); 22             } 23  24         ]]> 25     </fx:Script> 26     <fx:Declarations> 27         <!-- 将非可视元素(例如服务、值对象)放在此处 --> 28         <layer:GoogleMapLayer id="googleMapLayer"/> 29     </fx:Declarations> 30     <esri:Map id="esriMap" 31               logoVisible="false" 32               openHandCursorVisible="false" 33               scaleBarVisible="false" 34               zoomSliderVisible="false" load="esriMap_loadHandler(event)"> 35     </esri:Map> 36 </s:Application>
复制代码

    4.编译,把生成的文件部署到Web  服务器下运行,如下:

    5.源代码下载

总结

这样就把Gis应用最重要的部分“地图”加载进来了,剩下就就是对地图的操作,放大、缩小,增加点,线,面或其他图片图层等。以后还会对地图操作做进一步详细讲解。