arcgis api for android 叠加天地图

来源:互联网 发布:如何摆脱抑郁知乎 编辑:程序博客网 时间:2024/04/29 04:28

1.准备环境

ArcGIS Runtime SDK for Android v10.1.1,

下载地址:

http://download.csdn.net/detail/yu624774720hua/5216622

版本说明:

http://blog.csdn.net/arcgis_all/article/details/8530704


2.代码

Xml代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".TianDiTuActivity"    >    <com.esri.android.map.MapView        android:id="@+id/map_mapView"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </com.esri.android.map.MapView></LinearLayout>



package com.hvit.tianditu;/** * @author 作者姓名: 于华  * @E-mail yuh945@gmail.com * @version 创建时间:2012-10-13 下午05:21:43 * 图层切换 */public enum  TianDiTuTiledMapServiceType {/**     * 天地图矢量     * */VEC_C,    /**     * 天地图影像     * */    IMG_C,    /**     * 天地图矢量标注     * */    CVA_C,    /**     * 天地图影像标注     * */    CIA_C}

package com.hvit.tianditu;import java.util.Random;/** * @author 作者姓名: 于华  * @E-mail yuh945@gmail.com * @version 创建时间:2012-10-13 下午05:22:27 * 抓图类 */public class TDTUrl  {    private TianDiTuTiledMapServiceType _tiandituMapServiceType;    private int _level;    private int _col;    private int _row;    public TDTUrl(int level, int col, int row,TianDiTuTiledMapServiceType tiandituMapServiceType){        this._level=level;        this._col=col;        this._row=row;        this._tiandituMapServiceType=tiandituMapServiceType;    }    public String generatUrl(){        /**         * 天地图矢量、影像         * */        StringBuilder url=new StringBuilder("http://t");        Random random=new Random();        int subdomain = (random.nextInt(6) + 1);        url.append(subdomain);        switch(this._tiandituMapServiceType){        case VEC_C:              url.append(".tianditu.com/DataServer?T=vec_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level);            break;        case CVA_C:         url.append(".tianditu.com/DataServer?T=cva_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level);            break;        case CIA_C:         url.append(".tianditu.com/DataServer?T=cia_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level);            break;        case IMG_C:         url.append(".tianditu.com/DataServer?T=img_c&X=").append(this._col).append("&Y=").append(this._row).append("&L=").append(this._level);            break;            default:                return null;        }        return url.toString();    }   }


package com.hvit.tianditu;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.concurrent.RejectedExecutionException;import android.util.Log;import com.esri.android.map.TiledServiceLayer;import com.esri.core.geometry.Envelope;import com.esri.core.geometry.Point;import com.esri.core.geometry.SpatialReference;import com.esri.core.io.UserCredentials;/** * @author 作者姓名: 于华  * @E-mail yuh945@gmail.com * @version 创建时间:2012-10-13 下午05:23:07 * 类说明 */public class TianDiTuTiledMapServiceLayer extends TiledServiceLayer {    private TianDiTuTiledMapServiceType _mapType;    private TileInfo tiandituTileInfo;    public TianDiTuTiledMapServiceLayer() {        this(null, null,true);    }    public TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType mapType){        this(mapType, null,true);    }    public TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType mapType,UserCredentials usercredentials){        this(mapType, usercredentials, true);    }    public TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType mapType, UserCredentials usercredentials, boolean flag){        super("");        this._mapType=mapType;        setCredentials(usercredentials);                if(flag)            try            {                getServiceExecutor().submit(new Runnable() {                    public final void run()                    {                        a.initLayer();                    }                    final TianDiTuTiledMapServiceLayer a;                        {                a = TianDiTuTiledMapServiceLayer.this;                //super();            }                });                return;            }            catch(RejectedExecutionException _ex) { }    }    public TianDiTuTiledMapServiceType getMapType(){        return this._mapType;    }     protected void initLayer(){         this.buildTileInfo();         this.setFullExtent(new Envelope(-180,-90,180,90));         this.setDefaultSpatialReference(SpatialReference.create(4490));   //CGCS2000         //this.setDefaultSpatialReference(SpatialReference.create(4326));         this.setInitialExtent(new Envelope(90.52,33.76,113.59,42.88));         super.initLayer();     }      public void refresh()        {            try            {                getServiceExecutor().submit(new Runnable() {                    public final void run()                    {                        if(a.isInitialized())                            try                            {                                a.b();                                a.clearTiles();                                return;                            }                            catch(Exception exception)                            {                                Log.e("ArcGIS", "Re-initialization of the layer failed.", exception);                            }                    }                    final TianDiTuTiledMapServiceLayer a;                                {                    a = TianDiTuTiledMapServiceLayer.this;                    //super();                }                });                return;            }            catch(RejectedExecutionException _ex)            {                return;            }        }        final void b()                throws Exception            {                         }    @Override    protected byte[] getTile(int level, int col, int row) throws Exception {        /**         *          * */    byte[] result = null;try {ByteArrayOutputStream bos = new ByteArrayOutputStream();URL sjwurl = new URL(this.getTianDiMapUrl(level, col, row));HttpURLConnection httpUrl = null;BufferedInputStream bis = null;byte[] buf = new byte[1024];httpUrl = (HttpURLConnection) sjwurl.openConnection();httpUrl.connect();bis = new BufferedInputStream(httpUrl.getInputStream());while (true) {int bytes_read = bis.read(buf);if (bytes_read > 0) {bos.write(buf, 0, bytes_read);} else {break;}};bis.close();httpUrl.disconnect();result = bos.toByteArray();} catch (Exception ex) {ex.printStackTrace();}return result;    }    @Override    public TileInfo getTileInfo(){        return this.tiandituTileInfo;    }    /**     *      * */    private String getTianDiMapUrl(int level, int col, int row){                String url=new TDTUrl(level,col,row,this._mapType).generatUrl();        return url;    }        private void buildTileInfo()    {        Point originalPoint=new Point(-180,90);        double[] res={                1.40625,                0.703125,                0.3515625,                0.17578125,                0.087890625,                0.0439453125,                0.02197265625,                0.010986328125,                0.0054931640625,                0.00274658203125,                0.001373291015625,                0.0006866455078125,                0.00034332275390625,                0.000171661376953125,                8.58306884765629E-05,                4.29153442382814E-05,                2.14576721191407E-05,                1.07288360595703E-05,                5.36441802978515E-06,                2.68220901489258E-06,                1.34110450744629E-06        };        double[] scale={                400000000,                295497598.5708346,                147748799.285417,                73874399.6427087,                36937199.8213544,                18468599.9106772,                9234299.95533859,                4617149.97766929,                2308574.98883465,                1154287.49441732,                577143.747208662,                288571.873604331,                144285.936802165,                72142.9684010827,                36071.4842005414,                18035.7421002707,                9017.87105013534,                4508.93552506767,                2254.467762533835,                1127.2338812669175,                563.616940        };               int levels=21;        int dpi=96;        int tileWidth=256;        int tileHeight=256;        this.tiandituTileInfo=new com.esri.android.map.TiledServiceLayer.TileInfo(originalPoint, scale, res, levels, dpi, tileWidth,tileHeight);        this.setTileInfo(this.tiandituTileInfo);    }}

最后就是调用类了:

package com.hvit.tianditu;import android.app.Activity;import android.os.Bundle;import com.esri.android.map.MapView;import com.esri.android.map.event.OnZoomListener;public class TianDiTuActivity extends Activity {MapView mMapView ;TianDiTuTiledMapServiceLayer t_vec;TianDiTuTiledMapServiceLayer t_cva;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);mMapView  = (MapView) findViewById(R.id.map_mapView);;t_vec = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.VEC_C);mMapView.addLayer(t_vec);t_cva = new TianDiTuTiledMapServiceLayer(TianDiTuTiledMapServiceType.CVA_C);mMapView.addLayer(t_cva);mMapView.setOnZoomListener(new OnZoomListener() {@Overridepublic void preAction(float paramFloat1, float paramFloat2,double paramDouble) {// TODO Auto-generated method stub//缩放后//map_tidiOld2.clearTiles();//map_tidiOld2.refresh();}@Overridepublic void postAction(float paramFloat1, float paramFloat2,double paramDouble) {// TODO Auto-generated method stub//缩放前 防止标注重叠//map_tidiOld2.clearTiles();t_cva.refresh();}});    }@Override protected void onDestroy() { super.onDestroy(); }@Overrideprotected void onPause() {super.onPause();mMapView.pause(); }@Override protected void onResume() {super.onResume(); mMapView.unpause();}}

3.总结。

这样就完成了,但是存在问题。

1.标注层总是出现重复叠加,必须每次放大缩小是清除标注层,重新叠加。

2.分辨率和比例尺不准,测绘局不肯提供精确的。

3.Arcgis 叠加天地图,定位偏移很严重。(测绘局说正在修复)。


4.源代码下载。

造福人民:http://download.csdn.net/detail/yu624774720hua/5231037