在项目中可以经常需要动态加载一些图层,像投影地图服务、投影地图服务器。其实网上有大量这样的服务,比如天地图官网

随便点开一个服务,里面有相关的信息。那如何加载这样图层服务呢。

一、首先感谢这篇博文ArcGIS读取天地图出现错位的情况,这篇文章的下载链接也有许多有用的资料。加载天地图用到一个关键的自定义类TianDiTuLayer

package com.huang.tianditu.layer;

import java.util.Map;
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.SpatialReference; public class TianDiTuLayer extends TiledServiceLayer { private TianDiTuLayerInfo layerInfo; public TianDiTuLayer(int layerType) {
super(true);
this.layerInfo = LayerInfoFactory.getLayerInfo(layerType);
this.init();
} private void init() {
try {
getServiceExecutor().submit(new Runnable() {
public void run() {
TianDiTuLayer.this.initLayer();
}
});
} catch (RejectedExecutionException rejectedexecutionexception) {
Log.e("ArcGIS", "initialization of the layer failed.", rejectedexecutionexception);
}
} public byte[] getTile(int level, int col, int row) throws Exception {
String url = layerInfo.getUrl() + "?service=wmts&request=gettile&version=1.0.0&layer=" + layerInfo.getLayerName() + "&format=tiles&tilematrixset=" + layerInfo.getTileMatrixSet() + "&tilecol="
+ col + "&tilerow=" + row + "&tilematrix=" + (level + 1);
Map<String, String> map = null;
return com.esri.core.internal.io.handler.a.a(url, map);
} protected void initLayer() {
if (getID() == 0L) {
nativeHandle = create();
changeStatus(com.esri.android.map.event.OnStatusChangedListener.STATUS.fromInt(-1000));
} else {
this.setDefaultSpatialReference(SpatialReference.create(layerInfo.getSrid()));
Log.e("huang", layerInfo.toString()+"");
this.setFullExtent(new Envelope(layerInfo.getxMin(), layerInfo.getyMin(), layerInfo.getxMax(), layerInfo.getyMax()));
this.setTileInfo(new TileInfo(layerInfo.getOrigin(), layerInfo.getScales(), layerInfo.getResolutions(), layerInfo.getScales().length, layerInfo.getDpi(), layerInfo.getTileWidth(),
layerInfo.getTileHeight()));
super.initLayer();
}
} }

另外还有三个类,TianDiTuLayerInfo、LayerInfoFactory、TianDiTuLayerTypes这里就不贴代码了。资料下载

二、我们修改一下代码
1.常量类TDTConstant:

package com.huang.tianditu;

import com.esri.android.map.TiledServiceLayer.TileInfo;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point; public class TDTConstant { public static final String LAYER_NAME_VECTOR = "vec";
public static final String LAYER_NAME_VECTOR_ANNOTATION_CHINESE = "cva";
public static final String LAYER_NAME_VECTOR_ANNOTATION_ENGLISH = "eva";
public static final String LAYER_NAME_IMAGE = "img";
public static final String LAYER_NAME_IMAGE_ANNOTATION_CHINESE = "cia";
public static final String LAYER_NAME_IMAGE_ANNOTATION_ENGLISH = "eia";
public static final String LAYER_NAME_TERRAIN = "ter";
public static final String LAYER_NAME_TERRAIN_ANNOTATION_CHINESE = "cta"; public static final String TILE_MATRIX_SET_MERCATOR = "w";
public static final String TILE_MATRIX_SET_2000 = "c"; public static final Point ORIGIN_2000 = new Point(-180, 90);
public static final Point ORIGIN_MERCATOR = new Point(-20037508.3427892, 20037508.3427892); public static final int SRID_2000 = 4490;
public static final int SRID_MERCATOR = 102100; public static final double X_MIN_2000 = -180;
public static final double Y_MIN_2000 = -90;
public static final double X_MAX_2000 = 180;
public static final double Y_MAX_2000 = 90; public static final double X_MIN_MERCATOR = -20037508.3427892;
private static final double Y_MIN_MERCATOR = -20037508.3427892;
public static final double X_MAX_MERCATOR = 20037508.3427892;
public static final double Y_MAX_MERCATOR = 20037508.3427892; public static final int tileWidth = 256;
public static final int tileHeight = 256; public static final int dpi = 96; public static final double[] SCALES = { 2.958293554545656E8, 1.479146777272828E8, 7.39573388636414E7, 3.69786694318207E7, 1.848933471591035E7, 9244667.357955175, 4622333.678977588,
2311166.839488794, 1155583.419744397, 577791.7098721985, 288895.85493609926, 144447.92746804963, 72223.96373402482, 36111.98186701241, 18055.990933506204, 9027.995466753102,
4513.997733376551, 2256.998866688275 }; public static final double[] RESOLUTIONS_MERCATOR = { 78271.51696402048, 39135.75848201024, 19567.87924100512, 9783.93962050256, 4891.96981025128, 2445.98490512564, 1222.99245256282,
611.49622628141, 305.748113140705, 152.8740565703525, 76.43702828517625, 38.21851414258813, 19.109257071294063, 9.554628535647032, 4.777314267823516, 2.388657133911758, 1.194328566955879,
0.5971642834779395 }; public static final double[] RESOLUTIONS_2000 = { 0.7031249999891485, 0.35156249999999994, 0.17578124999999997, 0.08789062500000014, 0.04394531250000007, 0.021972656250000007,
0.01098632812500002, 0.00549316406250001, 0.0027465820312500017, 0.0013732910156250009, 0.000686645507812499, 0.0003433227539062495, 0.00017166137695312503, 0.00008583068847656251,
0.000042915344238281406, 0.000021457672119140645, 0.000010728836059570307, 0.000005364418029785169 }; public static final Envelope Envelope_2000 = new Envelope(TDTConstant.X_MIN_2000,
TDTConstant.Y_MIN_2000, TDTConstant.X_MAX_2000,
TDTConstant.Y_MAX_2000); public static final Envelope Envelope_MERCATOR = new Envelope(TDTConstant.X_MIN_MERCATOR,
TDTConstant.Y_MIN_MERCATOR, TDTConstant.X_MAX_MERCATOR,
TDTConstant.Y_MAX_MERCATOR); public static final TileInfo TileInfo_2000 = new TileInfo(TDTConstant.ORIGIN_2000,
TDTConstant.SCALES, TDTConstant.RESOLUTIONS_2000,
TDTConstant.SCALES.length, TDTConstant.dpi,
TDTConstant.tileWidth, TDTConstant.tileHeight); public static final TileInfo TileInfo_MERCATOR = new TileInfo(TDTConstant.ORIGIN_MERCATOR,
TDTConstant.SCALES, TDTConstant.RESOLUTIONS_MERCATOR,
TDTConstant.SCALES.length, TDTConstant.dpi,
TDTConstant.tileWidth, TDTConstant.tileHeight); }

2.TianDiTuLayer,代码如下 :

package com.huang.tianditu;

import java.util.Map;
import java.util.concurrent.RejectedExecutionException; import android.util.Log; import com.esri.android.map.TiledServiceLayer;
import com.esri.core.geometry.SpatialReference; public class TianDiTuLayer extends TiledServiceLayer { private TianDiTuInfo tianDiTuInfo; public TianDiTuLayer(TianDiTuInfo tianDiTuInfo) {
super(true);
this.tianDiTuInfo = tianDiTuInfo;
this.init();
} private void init() {
try {
getServiceExecutor().submit(new Runnable() {
public void run() {
TianDiTuLayer.this.initLayer();
}
});
} catch (RejectedExecutionException rejectedexecutionexception) {
Log.e("ArcGIS", "initialization of the layer failed.", rejectedexecutionexception);
}
} public byte[] getTile(int level, int col, int row) throws Exception {
String url = tianDiTuInfo.getUrl() + "?service=wmts&request=gettile&version=1.0.0&layer=" + tianDiTuInfo.getLayerName() + "&format=tiles&tilematrixset=" + tianDiTuInfo.getTileMatrixSet() + "&tilecol="
+ col + "&tilerow=" + row + "&tilematrix=" + (level + 1);
Map<String, String> map = null;
return com.esri.core.internal.io.handler.a.a(url, map);
} protected void initLayer() {
if (getID() == 0L) {
nativeHandle = create();
changeStatus(com.esri.android.map.event.OnStatusChangedListener.STATUS.fromInt(-1000));
} else {
this.setDefaultSpatialReference(SpatialReference.create(tianDiTuInfo.getSrid()));
this.setFullExtent(tianDiTuInfo.getEnvelope());
this.setTileInfo(tianDiTuInfo.getTileInfo());
super.initLayer();
}
} }

3.TianDiTuInfo实体类:

package com.huang.tianditu;

import com.esri.android.map.TiledServiceLayer.TileInfo;
import com.esri.core.geometry.Envelope; public class TianDiTuInfo { private String url;
private String layerName; private int minZoomLevel = 0;
private int maxZoomLevel = 17; private int srid; private String tileMatrixSet; private Envelope envelope;
private TileInfo tileInfo; public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getLayerName() {
return layerName;
} public void setLayerName(String layerName) {
this.layerName = layerName;
} public int getMinZoomLevel() {
return minZoomLevel;
} public void setMinZoomLevel(int minZoomLevel) {
this.minZoomLevel = minZoomLevel;
} public int getMaxZoomLevel() {
return maxZoomLevel;
} public void setMaxZoomLevel(int maxZoomLevel) {
this.maxZoomLevel = maxZoomLevel;
} public int getSrid() {
return srid;
} public void setSrid(int srid) {
this.srid = srid;
} public String getTileMatrixSet() {
return tileMatrixSet;
} public void setTileMatrixSet(String tileMatrixSet) {
this.tileMatrixSet = tileMatrixSet;
} public Envelope getEnvelope() {
return envelope;
} public void setEnvelope(Envelope envelope) {
this.envelope = envelope;
} public TileInfo getTileInfo() {
return tileInfo;
} public void setTileInfo(TileInfo tileInfo) {
this.tileInfo = tileInfo;
} }

4.Activity代码:

package com.huang.tianditu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener; import com.esri.android.map.MapView; public class TianDiTuLayerActivity extends Activity { private MapView mMapView; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mMapView = (MapView) findViewById(R.id.mapView); TianDiTuInfo tianDiTuInfo = new TianDiTuInfo();
tianDiTuInfo.setUrl("http://t0.tianditu.com/vec_c/wmts");
tianDiTuInfo.setLayerName(TDTConstant.LAYER_NAME_VECTOR);
tianDiTuInfo.setTileMatrixSet(TDTConstant.TILE_MATRIX_SET_2000);
tianDiTuInfo.setSrid(TDTConstant.SRID_2000);
tianDiTuInfo.setEnvelope(TDTConstant.Envelope_2000);
tianDiTuInfo.setTileInfo(TDTConstant.TileInfo_2000); mMapView.addLayer(new TianDiTuLayer(tianDiTuInfo));
} }

调用的地图服务相关信息
效果图:

Android GIS开发系列-- 入门季(12) 显示载天地图的更多相关文章

  1. Android GIS开发系列-- 入门季(14)FeatureLayer之范围查询

    Android GIS开发系列-- 入门季(5),这篇文章中,我们知道如何去查找要素.现在有一个需求,查找某点5000米范围的要素,那如何来做呢?首先我们需要在地图上画个5000米半径的圆,然后根据Q ...

  2. Android GIS开发系列-- 入门季(11) Callout气泡的显示

    一.气泡的简单显示 首先我们要获取MapView中的气泡,通过MapView的getCallout()方法获取一个气泡.看一下Callout的简单介绍: 大体的意思是通过MapView获取Callou ...

  3. Android GIS开发系列-- 入门季(1) 起点

    前言 这个系列,待最终完成更新,大家体谅点,第一版本全部是参考的网络教程,最近会逐步的细化更新为可以直接使用的情况. 本系列的开发基于AS (  Android Studio ), 和ArcGIS 的 ...

  4. Android GIS开发系列-- 入门季(2) MapView与图层介绍

    一.MapView MapView是Arcgis中的最基本的类,与高德地图SDK的MapView的重要性一样.MapView的创建有两种方法,一种是在Layout文件中直接写控件.一种是实例化,Map ...

  5. Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件

    Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...

  6. Android GIS开发系列-- 入门季(9) 定位当前的位置

    利用MapView定位当前的位置 这里要用到Arcgis中的LocationDisplayManager这个类,由于比较简单.直接上代码: LocationDisplayManager locatio ...

  7. Android GIS开发系列-- 入门季(8) Json与Geometry的相互转换

    在Android中json数据十分普遍,也很实用,在Arcgis中也同样支持Json数据,Json与Geometry可以相互转换,达到我们想要的数据. 一.Geometry转换成Json数据 这个实现 ...

  8. Android GIS开发系列-- 入门季(3) GraphicsLayer添加点、线、面

    GraphicsLayer是图形图层,可以自定义图形添加到地图上.调用GraphicsLayer的addGraphic方法就能添加图形,此方法要一个Graphic对象,此对象的构造方法是Graphic ...

  9. Android GIS开发系列-- 入门季(5) FeatureLayer加载本地shp文件与要素查询

    FeatureLayer是要素图层,也是Arcgis的主要图层.用这个图层可以加载本地的shp文件.下面我们看怎样加载shp文件到MapView中.查看ArcGis API可知FeatureLayer ...

随机推荐

  1. Android 在代码中安装 APK 文件

    废话不说,上代码 private void install(String filePath) { Log.i(TAG, "开始执行安装: " + filePath); File a ...

  2. 在Bootstrap中得模态框(modal)中下拉不能显示得问题

    $.fn.modal.Constructor.prototype.enforceFocus = function () { $("#insertModal").on("s ...

  3. [转]Android开发要看的网站(不断更新中)

    Android网址或Blog Android官网 身为Android开发者不知道这个网站就太说不过去了,上面有你任何你需要的东西 Android Developers Blog Android官网博客 ...

  4. sql server 2008 r2 无法定位到数据库文件目录

    像这样,选择数据库文件时, 无法定位到文件夹目录,子目录下的都不显示.明明选择的这个文件夹里还有很多子文件夹,却显示不了. 解决方法: 在此文件夹上右击,属性-安全 添加红框中的用户就可以了.

  5. JavaScript——blob、file、flieReader、createObjectURL

    https://blog.csdn.net/opengl_es/article/details/44336477 https://www.cnblogs.com/hhhyaaon/p/5928152. ...

  6. 洛谷 P2801 教主的魔法

    题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始都是 ...

  7. C++ 异常处理(try catch throw)、命名空间

    一.c++工具 模板(函数模板.类模板).异常处理.命名空间等功能是c++编译器的功能,语言本身不自带,这些功能已经成为ANSI C++标准了,建议所有的编译器都带这些功能,早期的c++是没有这些功能 ...

  8. chpasswd - 成批更新用户的口令

    总览 chpasswd [-e] 描述 chpasswd 从系统的标准输入读入用户的名称和口令,并利用这些信息来更新系统上已存在的用户的口令.在没有用 -e 这个开关选项的情况下,口令将按明文的形式接 ...

  9. 常见的HTTP相应状态码

    200:请求被正常处理204:请求被受理但没有资源可以返回206:客户端只是请求资源的一部分,服务器只对请求的部分资源执行GET方法,相应报文中通过Content-Range指定范围的资源.301:永 ...

  10. java混淆工具Jocky和Proguard

    java混淆工具有很多种,这里介绍Jocky和Proguard 一:Jocky是金蝶中间件技术领袖袁红岗先生的个人作品(旧有名称JOC).原本是方便Apusic 应用服务器的开发,现在开放出来,供大家 ...