在 gis 中,除了各种图层服务,就是底图了。这里记录一下使用 谷歌地图 作为底图并缓存到本地的方法。
上代码:
1、首先是自定义的地图服务 GoogleMapLayer
public class GoogleMapLayer extends TiledServiceLayer {
private int GoogleMapLayerType;
public GoogleMapLayer(int layerType) {
super(true);
this.GoogleMapLayerType = layerType;
this.init();
}
// 这里最大层级19,可以自定义(需要在下方的 scales 和 resolutions 中设置相对应的数据)
private int minLevel = 0;
private int maxLevel = 19;
private double[] scales = new double[] { 591657527.591555,
295828763.79577702, 147914381.89788899, 73957190.948944002,
36978595.474472001, 18489297.737236001, 9244648.8686180003,
4622324.4343090001, 2311162.217155, 1155581.108577, 577790.554289,
288895.277144, 144447.638572, 72223.819286, 36111.909643,
18055.954822, 9027.9774109999998, 4513.9887049999998, 2256.994353,
1128.4971760000001 };
private double[] resolutions = new double[] { 156543.03392800014,
78271.516963999937, 39135.758482000092, 19567.879240999919,
9783.9396204999593, 4891.9698102499797, 2445.9849051249898,
1222.9924525624949, 611.49622628138, 305.748113140558,
152.874056570411, 76.4370282850732, 38.2185141425366,
19.1092570712683, 9.55462853563415, 4.7773142679493699,
2.3886571339746849, 1.1943285668550503, 0.59716428355981721,
0.29858214164761665 };
private Point origin = new Point(-20037508.342787, 20037508.342787);
private int dpi = 96;
private int tileWidth = 256;
private int tileHeight = 256;
private void init() {
try {
getServiceExecutor().submit(new Runnable() {
public void run() {
GoogleMapLayer.this.initLayer();
}
});
} catch (RejectedExecutionException rejectedexecutionexception) {
Log.e("Google Map Layer", "initialization of the layer failed.",
rejectedexecutionexception);
}
}
protected byte[] getTile(int level, int col, int row) throws Exception {
if (level > maxLevel || level < minLevel) {
return new byte[0];
}
String s = "Galileo".substring(0, ((3 * col + row) % 8));
String url = "";
switch (GoogleMapLayerType) {
case GoogleMapLayerTypes.IMAGE_GOOGLE_MAP:
url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=s&hl=zh-CN&gl=cn&" +
"x=" + col + "&" +
"y=" + row + "&" +
"z=" + level + "&" +
"s=" + s;
break;
case GoogleMapLayerTypes.VECTOR_GOOGLE_MAP:
url = "http://mt2.google.cn/vt/lyrs=m@158000000&hl=zh-CN&gl=cn&" +
"x=" + col + "&" +
"y=" + row + "&" +
"z=" + level + "&" +
"s=" + s;
break;
case GoogleMapLayerTypes.TERRAIN_GOOGLE_MAP:
url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=t@131,r@227000000&hl=zh-CN&gl=cn&" +
"x=" + col + "&" +
"y=" + row + "&" +
"z=" + level + "&" +
"s=" + s;
break;
case GoogleMapLayerTypes.ANNOTATION_GOOGLE_MAP:
url = "http://mt" + (col % 4) + ".google.cn/vt/imgtp=png32&lyrs=h@169000000&hl=zh-CN&gl=cn&" +
"x=" + col + "&" +
"y=" + row + "&" +
"z=" + level + "&" +
"s=" + s;
break;
}
// 这里是设置缓存到本地的路径 可以自定义
String imgPtPath=MainActivity.defaultPath+"t"+GoogleMapLayerType+"/";
File dirFile = new File(imgPtPath);
if(!dirFile.exists()){
dirFile.mkdirs();
}
String imgPath=imgPtPath+"l"+level+"/";
String imgName ="c"+col+"r"+row+".png";
if(MainActivity.isConnect()){
Map map = null;
byte[] btImg= com.esri.core.internal.io.handler.a.a(url, map);
saveFile(imgPath,btImg,imgName);
return btImg;
}else {
Bitmap bitmap = BitmapFactory.decodeFile(imgPath+imgName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
}
public void saveFile(String filePath,byte[] data, String fileName) throws IOException {
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);// bitmap
File dirFile = new File(filePath);
if(!dirFile.exists()){
dirFile.mkdir();
}
// 检测是否有 .nomedia 文件,该文件防止相册等媒体软件扫描离线地图图片,以免造成不必要的麻烦
File nomediaFile = new File(filePath+".nomedia");
if (!nomediaFile.exists()){
nomediaFile.createNewFile();
}
File myCaptureFile = new File(filePath + fileName);
// 判断离线的图片是否已经存在,已经存在的地图不用再次下载(节省流量)
if (myCaptureFile.exists()){
return;
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
mBitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
bos.flush();
bos.close();
}
protected SpatialReference getSptialReference(){
return this.getDefaultSpatialReference();
}
protected void initLayer() {
if (getID() == 0L) {
nativeHandle = create();
changeStatus(com.esri.android.map.event.OnStatusChangedListener.STATUS
.fromInt(-1000));
} else {
this.setDefaultSpatialReference(SpatialReference.create(102113));
this.setFullExtent(new Envelope(-22041257.773878,
-32673939.6727517, 22041257.773878, 20851350.0432886));
this.setTileInfo(new TileInfo(origin, scales, resolutions,
scales.length, dpi, tileWidth, tileHeight));
super.initLayer();
}
}
}
2、接着是谷歌地图的类型
public interface GoogleMapLayerTypes {
/**
* 矢量图
*/
final int VECTOR_GOOGLE_MAP = 1;
/**
* 影像图
*/
final int IMAGE_GOOGLE_MAP = 2;
/**
* 地形图
*/
final int TERRAIN_GOOGLE_MAP = 3;
/**
* 标注
*/
final int ANNOTATION_GOOGLE_MAP = 4;
}
在代码中使用,很简单:获取地图服务实例并添加到mapview中就行
GoogleMapLayer googleLayer = new GoogleMapLayer(GoogleMapLayerTypes.VECTOR_GOOGLE_MAP);
mMapView.addLayer(googleLayer);
仅供大家学习、参考。
参考文章:http://blog.csdn.net/u014014578/article/details/21476395