使用osmdroid載入谷歌、高德、天地圖等瓦片地圖
阿新 • • 發佈:2018-12-16
研究了一星期多的Osmdroid,深感這個開源的android地圖包強大,因為使用其他瓦片涉及智慧財產權,請大家謹慎使用,本文只做研究測試。
新建GoogleTileSource類繼承org.osmdroid.tileprovider.tilesource.TileSourceFactory類,具體對應的資源都有註釋
public class GoogleTileSource extends TileSourceFactory {
//谷歌衛星混合
public static final OnlineTileSourceBase GoogleHybrid = new XYTileSource ("Google-Hybrid",
0, 19, 512, ".png", new String[]{
"http://mt0.google.cn",
"http://mt1.google.cn",
"http://mt2.google.cn",
"http://mt3.google.cn",
}) {
@Override
public String getTileURLString(long pMapTileIndex) {
Log. d("url", getBaseUrl() + "/vt/lyrs=y&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex));
return getBaseUrl() + "/vt/lyrs=y&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
//谷歌衛星
public static final OnlineTileSourceBase GoogleSat = new XYTileSource("Google-Sat",
0, 19, 512, ".png", new String[]{
"http://mt0.google.cn",
"http://mt1.google.cn",
"http://mt2.google.cn",
"http://mt3.google.cn",
}) {
@Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl() + "/vt/lyrs=s&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
//谷歌地圖
public static final OnlineTileSourceBase GoogleRoads = new XYTileSource("Google-Roads",
0, 18, 512, ".png", new String[]{
"http://mt0.google.cn",
"http://mt1.google.cn",
"http://mt2.google.cn",
"http://mt3.google.cn",
}) {
@Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl() + "/vt/lyrs=m&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
//谷歌地形
public static final OnlineTileSourceBase GoogleTerrain = new XYTileSource("Google-Terrain",
0, 16, 512, ".png", new String[]{
"http://mt0.google.cn",
"http://mt1.google.cn",
"http://mt2.google.cn",
"http://mt3.google.cn",
}) {
@Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl() + "/vt/lyrs=t&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
//谷歌地形帶標註
public static final OnlineTileSourceBase GoogleTerrainHybrid = new XYTileSource("Google-Terrain-Hybrid",
0, 16, 512, ".png", new String[]{
"http://mt0.google.cn",
"http://mt1.google.cn",
"http://mt2.google.cn",
"http://mt3.google.cn",
}) {
@Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl() + "/vt/lyrs=p&scale=2&hl=zh-CN&gl=CN&src=app&x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
//高德地圖
public static final OnlineTileSourceBase AutoNaviVector = new XYTileSource("AutoNavi-Vector",
0, 20, 256, ".png", new String[]{
"https://wprd01.is.autonavi.com/appmaptile?",
"https://wprd02.is.autonavi.com/appmaptile?",
"https://wprd03.is.autonavi.com/appmaptile?",
"https://wprd04.is.autonavi.com/appmaptile?",
}) {
@Override
public String getTileURLString(long pMapTileIndex) {
return getBaseUrl() + "x=" + MapTileIndex.getX(pMapTileIndex) + "&y=" + MapTileIndex.getY(pMapTileIndex) + "&z="
+ MapTileIndex.getZoom(pMapTileIndex)+"&lang=zh_cn&size=1&scl=1&style=7<ype=7";
}
};
//影像地圖 _W是墨卡託投影 _c是國家2000的座標系
public static final OnlineTileSourceBase tianDiTuImgTileSource = new XYTileSource("TianDiTuImg", 1, 18, 768, ".png",
new String[]{"http://t1.tianditu.com/DataServer?T=img_w",
"http://t2.tianditu.com/DataServer?T=img_w",
"http://t3.tianditu.com/DataServer?T=img_w",
"http://t4.tianditu.com/DataServer?T=img_w",
"http://t5.tianditu.com/DataServer?T=img_w",
"http://t6.tianditu.com/DataServer?T=img_w"}) {
@Override
public String getTileURLString(final long pMapTileIndex) {
return getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
+ "&L=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
//影像標註 _W是墨卡託投影 _c是國家2000的座標系
public static final OnlineTileSourceBase tianDiTuCiaTileSource = new XYTileSource("TianDiTuCia", 1, 18, 768, ".png",
new String[]{"http://t1.tianditu.com/DataServer?T=cia_w",
"http://t2.tianditu.com/DataServer?T=cia_w",
"http://t3.tianditu.com/DataServer?T=cia_w",
"http://t4.tianditu.com/DataServer?T=cia_w",
"http://t5.tianditu.com/DataServer?T=cia_w",
"http://t6.tianditu.com/DataServer?T=cia_w"}) {
@Override
public String getTileURLString(final long pMapTileIndex) {
Log.d("url", getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
+ "&L=" + MapTileIndex.getZoom(pMapTileIndex));
return getBaseUrl() + "&X=" + MapTileIndex.getX(pMapTileIndex) + "&Y=" + MapTileIndex.getY(pMapTileIndex)
+ "&L=" + MapTileIndex.getZoom(pMapTileIndex);
}
};
}
然後在主程式MainActivity中的mapview中使用如下程式碼:
map = (MapView) findViewById(R.id.map);
map.setTileSource(GoogleTileSource.MAPNIK)
因為是繼承的,所以原來包中自帶的OpenStreetMap、OpenSea的瓦片資源都可以繼續引用。
問題:
- 為了保密高德、谷歌.cn、天地圖使用的是GCJ02(火星座標系),手機gps資料是WGS-84座標系,需要進行轉換。
- 為什麼沒有百度,因為百度的瓦片分割和高德谷歌都不一樣,還有百度使用的自己的BD09座標系 即百度座標系,估計GCJ02座標系經加密後的座標系。