本系列专题本来上次想一次性写完,无奈工作太忙,上月就中断了一个月,这个月补上两篇,在此对各位表示抱歉。
本实例主要是针对四川地理信息中心的切片,如果利用ArcGIS for Android API进行解析,并读入作用底图。
1)基础准备:下载Java版的Json解析库(http://code.google.com/p/json-simple/)、熟悉四川地理信息中心切片规则
2)关于四川地理信息中心的切片规则这就不做介绍了,在以前专题已介绍过,详见:http://www.scgis.net/
3)代码说明
AgsLOD.java:切片等级信息定义文件,在这定义了比例尽、等级、分辨率
package com.esri.arcgis.android.samples;import com.esri.core.internal.d.c;public class AgsLOD extends c {private static final long serialVersionUID = 4341699179151728883L; private int level; private double resolution; private double scale; public AgsLOD(int level, double scale, double resolution) { super(); this.level = level; this.scale = scale; this.resolution = resolution; } public int a() { return this.level; } public double b() { return this.resolution; } public double c() { return this.scale; }}GetToken.java:通过Web请求提交用户名跟密码,获取Token字符口串,并对字符串进行解析,用于获取地图切片。
package com.esri.arcgis.android.samples;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;public class GetToken { //get token public String sendPost(String url) { StringBuilder result = new StringBuilder(); try { URL httpurl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) httpurl .openConnection(); httpConn.setDoInput(true); BufferedReader in = new BufferedReader(new InputStreamReader( httpConn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { result .append(line); } //System.out.println(result); in.close(); //get token return result.toString().split("\"")[15]; } catch (Exception e) { e.printStackTrace(); } return null; }}
SCGISModel.java:切片参数定义文件,包括切片的空间参考等基础信息
package com.esri.arcgis.android.samples;import java.io.BufferedInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.net.HttpURLConnection;import java.net.URL;import com.esri.core.geometry.Envelope;import com.esri.core.geometry.SpatialReference;import com.esri.core.internal.d.k;import com.esri.core.map.TiledLayerModel;public class SCGISModel extends TiledLayerModel { private static final long serialVersionUID = 7726567118839553087L; private String location; private String token; public SCGISModel(String location, SpatialReference sr, Envelope full, k tileInfo) { super(sr, full, tileInfo); this.location = location; this.token=new GetToken().sendPost("http://www.scgis.net.cn/imap/iMapServer/Token/getToken?username=test20110703&password=test20110703&IpAddress=&TimeSpan=100M&st=1302073818076"); } @Override public byte[] getTile(int level, int row, int col) throws Exception { byte[] result = null; try { int levelCustom = level + 1; String bundlesDir = location + levelCustom + File.separator + row + File.separator + col + "?token="+this.token; ; ByteArrayOutputStream bos = new ByteArrayOutputStream(); URL sjwurl = new URL(bundlesDir); 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; }}
SCGISMapServices.java:继承tiledLayer,自定义切片类。
package com.esri.arcgis.android.samples;import java.util.ArrayList;import android.content.Context;import android.util.AttributeSet;import com.esri.android.map.TiledLayer;import com.esri.core.geometry.Envelope;import com.esri.core.geometry.Point;import com.esri.core.geometry.SpatialReference;import com.esri.core.internal.d.c;import com.esri.core.internal.d.k;import com.esri.core.map.TiledLayerModel;public class SCGISMapServices extends TiledLayer { private String location = "http://www.scgis.net.cn/iMap/iMapServer/NewRest/services/SCTileMap/tile/"; private SpatialReference spatialReference = SpatialReference.create(4326); private Envelope fullExtent = new Envelope(95.918, 24.898, 110.14, 35.209); private k tileInfo; public SCGISMapServices(Context context, AttributeSet attrs) { super(context, attrs); try { init(); } catch (Exception ex) { ex.printStackTrace(); } } @Override protected TiledLayerModel initModel() throws Exception { return new SCGISModel(location, spatialReference, fullExtent, tileInfo); } private void init() { try { tileInfo = new k(); tileInfo.f = new Point(-180, 90); tileInfo.a = 256; tileInfo.b = 256; tileInfo.h = new ArrayList<c>(); double resolution = 0.010998662747496; double scale=4622333.68; for (int j = 0, jcount = 11; j < jcount; j++) { int level = j; tileInfo.h.add(new AgsLOD(level, scale, resolution)); resolution/=2; scale/=2; } } catch (Exception e) { e.printStackTrace(); } }}4)效果图如下:
如果需代码学习的同学,在本系列专题之五后面留下邮箱,可将此系列专题的所有代码提供出来供大学学习!