前面讲到在Worldwind上加载Geoserver影像,现在记录下载worldwind上加载天地图。
要加载天地图影像,首先需要在天地图官网申请KEY值
进入官网,创建应用
填写SHA1
带有星号的是必填项。获取SHA1值的方法就不在赘述了。填写好后点击提交,就会生成key值。
进入到开发程序中。
public class TiandituLayers extends RenderableLayer implements TileFactory {
String urlAddress = "";
public static final int TIANDITU_IMAGE_LAYER = 1;
public static final int TIANDITU_IMAGE_ANNOTATION_LAYER = 2;
public static final int TIANDITU_VECTOR_LAYER = 3;
public static final int TIANDITU_VECTOR_ANNOTATION_LAYER = 4;
//天地图影像图层地址
private static final String IMAGE_URL = "http://t0.tianditu.gov.cn/img_c/wmts";
//天地图影像图层注记地址
private static final String IMAGE_ANNOTATION_URL = "http://t0.tianditu.gov.cn/cia_c/wmts";
//天地图矢量图层地址
private static final String VECTOR_URL = "http://t0.tianditu.gov.cn/vec_c/wmts";
//天地图矢量图层注记地址
private static final String VECTOR_ANNOTATION_URL = "http://t0.tianditu.gov.cn/cva_c/wmts";
//天地图申请的key:发现自2019年1月1日起地图API及服务接口调用都需要获得开发授权,所以就需要在请求ip地址之后加上申请到的key值
private static final String TIANDITU_KEY = "申请到的Key值";
public TiandituLayers(String serviceAddress){
if (serviceAddress == null) {
throw new IllegalArgumentException(
Logger.logMessage(Logger.ERROR, "BlueMarbleLandsatLayer", "constructor", "missingServiceAddress"));
}
urlAddress = serviceAddress;
//tiandituTileFactory = new WmtsTileFactory();
// Configure this layer's level set to capture the entire globe at 15m resolution.
double metersPerPixel = 15;
double radiansPerPixel = metersPerPixel / WorldWind.WGS84_SEMI_MAJOR_AXIS;
LevelSetConfig levelsConfig = new LevelSetConfig(null, 45, 16, 256, 256);
//levelsConfig.numLevels = levelsConfig.numLevelsForResolution(radiansPerPixel);
this.setDisplayName("TiandituSat");
this.setPickEnabled(false);
TiledSurfaceImage surfaceImage = new TiledSurfaceImage();
surfaceImage.setLevelSet(new LevelSet(levelsConfig));
surfaceImage.setTileFactory(this);
surfaceImage.setImageOptions(new ImageOptions(WorldWind.RGB_565)); // reduce memory usage by using a 16-bit configuration with no alpha
this.addRenderable(surfaceImage);
}
@Override
public Tile createTile(Sector sector, Level level, int row, int column) {
ImageTile tile = new ImageTile(sector, level, row, column);
//String urlString = urlAddress;//this.urlForTile(level.levelNumber, row, column);
int row1 = (int) Math.pow(2, (level.levelNumber + 2)) - 1 - row;//计算行列和级数
int col1 = column;
int level1 = level.levelNumber+3;
String serverURL = urlAddress.replaceFirst("0", String.valueOf((int)(Math.random() * 8)));//由于服务器端采用了集群技术,http://tile0/同http://tile7/取的是同一图片
String urlString = "";
if (urlAddress.contains("img_c")) {
urlString = getLayerUrl(serverURL, "img", level1, row1, col1);
} else if (urlAddress.contains("vec_c")){
urlString = getLayerUrl(serverURL, "vec", level1, row1, col1);
} else if (urlAddress.contains("cia_")) {
urlString = getLayerUrl(serverURL, "cia", level1, row1, col1);
} else if (urlAddress.contains("cva_c")) {
urlString = getLayerUrl(serverURL, "cva", level1, row1, col1);
}
if (urlString != null) {
tile.setImageSource(ImageSource.fromUrl(urlString));
}
return tile;
}
//定义url
private String getLayerUrl(String serverURL, String layerPattern, int level, int row, int col) {
return serverURL + "?service=wmts" +
"&request=gettile" +
"&version=1.0.0" +
"&layer=" + layerPattern +
"&Style=default" +
"&Format=tiles" +
"&TileMatrixSet=c" +
"&TileMatrix=" + level +
"&TileRow=" + row +
"&TileCol=" + col +
"&tk=" + TIANDITU_KEY;
}
}
之后在创建worldwind的窗口(createWorldWindow())方法中添加应用即可。
public WorldWindow createWorldWindow() {
worldWindow = new WorldWindow(getApplicationContext());
worldWindow.getLayers().addLayer(new BackgroundLayer());
worldWindow.getGlobe().getElevationModel().addCoverage(new BasicElevationCoverage());
//去掉了suspension图层
//worldWindow.getLayers().addLayer(new BlueMarbleLandsatLayer());
//定义进来虚拟地球界面的观测点
worldWindow.getNavigator().setLatitude(39.5960);
worldWindow.getNavigator().setLongitude(116.7719);
worldWindow.getNavigator().setAltitude(0.2e8);
//addBasicLayer();//添加的Geoserver图层
tiandituLayer = new TiandituLayers("http://t0.tianditu.gov.cn/img_c/wmts");//添加天地图的影像
tiandituLayer2 = new TiandituLayers("http://t0.tianditu.gov.cn/cia_c/wmts");//添加天地图的矢量图层
worldWindow.getLayers().addLayer(tiandituLayer);
worldWindow.getLayers().addLayer(tiandituLayer2);
locateLayer = new RenderableLayer("VectorTest");
worldWindow.getLayers().addLayer(locateLayer);
return worldWindow;
}
注意如果加载时出现418代码,就需要注意下获取到的key值是否正确。自2019年1月1日起地图API及服务接口调用都需要获得开发授权,所以就需要在请求ip地址之后加上申请到的key值。