昨日看到一个牛人发的一篇博文,内容是利用arcgis api for flex读取自定义瓦片地图。我一想,能不能做Flex4.6手机项目读取本地地图缓存。经过两天的捣鼓,终于在我的爱机上显示预先切好的瓦片地图,欢喜。贴上代码:
MyLayer.as package map { import com.esri.ags.SpatialReference; import com.esri.ags.geometry.Extent; import com.esri.ags.geometry.MapPoint; import com.esri.ags.layers.TiledMapServiceLayer; import com.esri.ags.layers.supportClasses.LOD; import com.esri.ags.layers.supportClasses.TileInfo; import flash.filesystem.File; import flash.net.URLRequest; public class MyLayer extends TiledMapServiceLayer { public function PortlandTiledMapServiceLayer() { super(); buildTileInfo(); // to create our hardcoded tileInfo setLoaded(true); // Map will only use loaded layers } private var _tileInfo:TileInfo = new TileInfo(); // see buildTileInfo() public var filePath:File=File.documentsDirectory; private var _baseURL:String =filePath.url+"/BaseMap2/Layers/_alllayers/";//filePath.url为地图瓦片地图目录 //-------------------------------------------------------------------------- // // Overridden properties // fullExtent() // initialExtent() // spatialReference() // tileInfo() // units() // //-------------------------------------------------------------------------- //---------------------------------- // fullExtent // - required to calculate the tiles to use //---------------------------------- override public function get fullExtent():Extent { return new Extent(106.83278544323, 19.5628611240401, 117.999305409068, 26.6235368076667, new SpatialReference(4326)); } //---------------------------------- // initialExtent // - needed if Map doesn't have an extent //---------------------------------- override public function get initialExtent():Extent { return new Extent(112.511576567339, 21.9382229904827, 114.171488565007, 23.0936892549139, new SpatialReference(4326)); } //---------------------------------- // spatialReference // - needed if Map doesn't have a spatialReference //---------------------------------- override public function get spatialReference():SpatialReference { return new SpatialReference(4326); } //---------------------------------- // tileInfo //---------------------------------- override public function get tileInfo():TileInfo { return _tileInfo; } //---------------------------------- // units // - needed if Map doesn't have it set //---------------------------------- override public function get units():String { return "esriDecimalDegrees"; } //-------------------------------------------------------------------------- // // Overridden methods // getTileURL(level:Number, row:Number, col:Number):URLRequest // //-------------------------------------------------------------------------- override protected function getTileURL(level:Number, row:Number, col:Number):URLRequest { var url:String = _baseURL + "/L" + padString(String(level), 2, "0") + "/R" + padString(row.toString(16), 8, "0") + "/C" + padString(col.toString(16), 8, "0") + ".png"; return new URLRequest(url); } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- private function buildTileInfo():void { _tileInfo.height = 256; _tileInfo.width = 256; _tileInfo.origin = new MapPoint(-400, 400); _tileInfo.spatialReference = new SpatialReference(4326); _tileInfo.lods = [ new LOD(0, 0.0020766205141791532,1000000), new LOD(1, 0.00062298615425374602,300000) ]; } private function padString(text:String, size:int, ch:String):String { while (text.length < size) { text = ch + text; } return text; } } }
offline.mxml <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" applicationDPI="160" xmlns:ns="http://www.esri.com/2008/ags" xmlns:map="map.*"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <ns:Map id="map" scaleBarVisible="false"> <map:MyLayer id="virtualTiles" fadeInFrameCount="12"/> </ns:Map> </s:Application>