public class GaoDeLayer extends TiledServiceLayer {
private final int GaoDeLayerType;
private final String cachePath;// 缓存目录
private boolean whetheCachedMap = false;// 是否缓存地图
private String offLineMapPath;// 离线地图目录
public GaoDeLayer(int layerType, boolean whetheCachedMap,
String cachePath,String offLineMapPath) {
super(true);
this.whetheCachedMap = whetheCachedMap;
this.cachePath = cachePath;
this.GaoDeLayerType = layerType;
this.offLineMapPath = offLineMapPath;
this.init();
}
private final 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 final 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 final Point origin = new Point(-20037508.342787, 20037508.342787);
private void init() {
try {
getServiceExecutor().submit(new Runnable() {
public void run() {
GaoDeLayer.this.initLayer();
}
});
} catch (RejectedExecutionException rejectedexecutionexception) {
Log.e("GaoDe Map Layer", "initialization of the layer failed.",
rejectedexecutionexception);
}
}
@Override
protected byte[] getTile(int level, int col, int row) throws Exception {
int maxLevel = 17;
int minLevel = 0;
if (level > maxLevel || level < minLevel) {
return new byte[0];
}
byte[] tiles = new byte[0];
//先从离线地图中读取
switch (GaoDeLayerType) {
case GaoDeLayerTypes.IMAGE_GAODE_MAP:
tiles = TileFactory.readGaoDeOffLineMapTiles(offLineMapPath,level, col, row,1);
break;
case GaoDeLayerTypes.IMAGE_GAODE_MARKING_MAP:
tiles = TileFactory.readGaoDeOffLineMapTiles(offLineMapPath,level, col, row,2);
break;
}
if (RegexUtils.isNotNull(tiles)) {
return tiles;
}
//再从缓存中读取
tiles = TileFactory.readTiles(cachePath, level, col, row);
if (RegexUtils.isNotNull(tiles)) {
return tiles;
}
String s = "Galileo".substring(0, ((3 * col + row) % 8));
String url = "";
switch (GaoDeLayerType) {
case GaoDeLayerTypes.IMAGE_GAODE_MAP:
url = "https://wprd01.is.autonavi.com/appmaptile?x="+col+"&y="+row+"&z="+level+"&lang=zh_cn&size="+s+"&scl=1"+"&style=6";
break;
case GaoDeLayerTypes.IMAGE_GAODE_MARKING_MAP:
url = "https://wprd01.is.autonavi.com/appmaptile?x="+col+"&y="+row+"&z="+level+"&lang=zh_cn&size="+s+"&scl=1"+"&style=8";
break;
}
Map<String, String> map = null;
tiles = com.esri.core.internal.io.handler.a.a(url, map);
TileFactory.writeTiles(whetheCachedMap, cachePath, tiles, level, col,
row);
return tiles;
}
@Override
protected void initLayer() {
int dpi = 96;
int tileWidth = 256;
int tileHeight = 256;
if (getID() == 0L) {
nativeHandle = create();
changeStatus(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();
}
}
}
地图展示后勾画地块
勾画的地块获取各个Point点(List<Point>)
拼接思路 获取最大最小xy点 组成四边形 遍历拼接url
if (null != project && !project.isEmpty()) {
Map<Integer, int[]> levelToBounds = new HashMap<>();
for (int level = 0; level <= 18; level++) {
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxY = Integer.MIN_VALUE;
// 遍历所有点,计算每个层级的边界
for (Point projectedPoint : project) {
int[] tileCoordinates = getTileNumber(projectedPoint.getX(), projectedPoint.getY(), level);
minX = Math.min(minX, tileCoordinates[0]);
minY = Math.min(minY, tileCoordinates[1]);
maxX = Math.max(maxX, tileCoordinates[0]);
maxY = Math.max(maxY, tileCoordinates[1]);
}
// 存储层级与边界信息
levelToBounds.put(level, new int[]{minX, minY, maxX, maxY});
}
List<String> urlList = new ArrayList<>();
for (Map.Entry<Integer, int[]> entry : levelToBounds.entrySet()) {
int level = entry.getKey();
int[] bounds = entry.getValue();
for (int x = bounds[0]; x <= bounds[2]; x++) {
for (int y = bounds[1]; y <= bounds[3]; y++) {
// 拼接瓦片请求的URL
String tileUrl = "https://wprd01.is.autonavi.com/appmaptile?x=" + x + "&y=" + y + "&z=" + level + "&lang=zh_cn&size=Galil&scl=1&style=6";
urlList.add(tileUrl);
}
}
}
}
public static int[] getTileNumber(double longitude, double latitude, int zoomLevel) {
double longitudeRad = longitude * Math.PI / 180;
double latitudeRad = latitude * Math.PI / 180;
double y = Math.log(Math.tan((Math.PI / 4) + (latitudeRad / 2)));
int tileX = (int) Math.floor((longitudeRad + Math.PI) / (2 * Math.PI) * Math.pow(2, zoomLevel));
int tileY = (int) Math.floor((1 - (y / Math.PI)) / 2 * Math.pow(2, zoomLevel));
return new int[]{tileX, tileY};
}
获取到url就可以去下载了