Arcgis for android 如何重写TiledServiceLayer
问题:在ArcGIS Runtime SDK for Android10.1版本中怎么样重写TiledServiceLayer,以实现读取自定义的瓦片数据。
我的代码如下:
package com.esri.arcgis.android.samples.helloworld;
import java.io.ByteArrayOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import com.esri.android.map.TiledServiceLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.SpatialReference;
public class customTiledLayer extends TiledServiceLayer {
public customTiledLayer(String url, boolean initLayer) {
super(url);
// TODO Auto-generated constructor stub'
Envelope e = new Envelope(96.8064823,25.68096106,109.1252279,34.75215408);
Envelope fulle = new Envelope(-180,-90,180,90);
this.setInitialExtent(e); //初始范围
this.setFullExtent(fulle); //最大范围
this.setDefaultSpatialReference(SpatialReference.create(4326));
Point pPoint =new Point(-180,90);
double [] scale = new double[15]; //比例尺
double [] res = new double[15]; //分辨率
scale[0] = 0.0219726567661385;
res[0] = 9234300;
for(int i=1;i<15;i++)
{
scale[i]=scale[i-1]/2.0;
res[i]= res[i-1]/2.0;
}
TileInfo pTileInfo = new TileInfo(pPoint,scale, res, 15, 96, 256, 256);
this.setTileInfo(pTileInfo);
//return customTiledLayer.this.getTile(level, row, col)
}
protected byte[] getTile(int level, int row, int col) throws Exception
{
byte[] arrayOfByte = null;
HttpEntity localHttpEntity = null;
ByteArrayOutputStream localByteArrayOutputStream = null;
try
{
System.out.println("gettiles:col" + String.valueOf(col) + "row" + String.valueOf(row) + "level" + String.valueOf(level));
String cachepath = getUrl() + "/tile" + "/" + String.valueOf(level) + "/" + String.valueOf(row) + "/" + String.valueOf(col);
HttpPost post = new HttpPost(cachepath);
HttpResponse response = new DefaultHttpClient().execute(post);
localHttpEntity = response.getEntity();
localByteArrayOutputStream = new ByteArrayOutputStream();
localHttpEntity.writeTo(localByteArrayOutputStream);
arrayOfByte = localByteArrayOutputStream.toByteArray();
localByteArrayOutputStream.flush();
return arrayOfByte;
}
catch (Exception e)
{
System.out.println("gettilesfailed:" + e.toString());
throw e;
}
finally
{
if (localByteArrayOutputStream != null)
localByteArrayOutputStream.close();
if (localHttpEntity != null) {
localHttpEntity.consumeContent();
}
}
}
}
但是一直不能出图,请大家帮我看哈,是哪里有问题的哈。谢谢各位大牛了。 android arcgis --------------------编程问答-------------------- 我和你的类似,也是不出图啊。不知道楼主是否已经解决问题了。我参考的那个加载天地图的demo,竟然还有一个辅助的jar包文件。发现那个demo很是扯淡。 --------------------编程问答-------------------- 您好,您这个实现了吗? --------------------编程问答--------------------
package com.gtmap.android.hydro.jihua.map;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
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;
/**
* 天地图
*/
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)); //GCS_WGS_1984
// this.setInitialExtent(new Envelope(90.52,33.76,113.59,42.88));
this.setInitialExtent(new Envelope(118.91208162289873,28.43301753729623,120.89833669751555,29.7232802677077));
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){
/**
* 天地图矢量、影像
* */
StringBuilder url=new StringBuilder("http://t");
Random random=new Random();
int subdomain = (random.nextInt(6) + 1);
url.append(subdomain);
switch(this._mapType){
case VEC_C:
url.append(".tianditu.com/DataServer?T=vec_c&X=").append(col).append("&Y=").append(row).append("&L=").append(level);
break;
case CVA_C:
url.append(".tianditu.com/DataServer?T=cva_c&X=").append(col).append("&Y=").append(row).append("&L=").append(level);
break;
case CIA_C:
url.append(".tianditu.com/DataServer?T=cia_c&X=").append(col).append("&Y=").append(row).append("&L=").append(level);
break;
case IMG_C:
url.append(".tianditu.com/DataServer?T=img_c&X=").append(col).append("&Y=").append(row).append("&L=").append(level);
break;
default:
return null;
}
return url.toString();
}
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);
}
public enum TianDiTuTiledMapServiceType {
/**
* 天地图矢量
* */
VEC_C,
/**
* 天地图影像
* */
IMG_C,
/**
* 天地图矢量标注
* */
CVA_C,
/**
* 天地图影像标注
* */
CIA_C
}
}
这个是加载天地图的,LZ可能是拼出的那个地址访问不到了吧,你可以试试。
补充:移动开发 , Android