在上一章中,我们学习了怎么在Android设备上加载WorldWind地球,在这一章中我们将学习怎么在worldwind上加载自己在Geoserver上发布的影像。
注意:安卓高版本可能会出现加载失败的情况,所以需提前在AndroidManifest中的中添加一句话
android:usesCleartextTraffic="true"
如下图所示。具体原因可以参考我的[另一篇文章]。(https://blog.csdn.net/nanjumufeng/article/details/103011017)
现在可以继续下面的开发了。
接下来,将第一章中MainActivity中onCreate方法中的做下改变
WorldWindow worldWindowView = createWorldWindow();
//worldWindowView.setWorldWindowController(new BasicWorldWindowController());
globelayout=findViewById(R.id.globe);
globelayout.addView(worldWindowView);
接下来创建地球窗口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(38.876828);
worldWindow.getNavigator().setLongitude(116.458833);
worldWindow.getNavigator().setAltitude(2000);
addBasicLayer();//添加的Geoserver图层
locateLayer = new RenderableLayer("VectorTest");
worldWindow.getLayers().addLayer(locateLayer);
return worldWindow;
}
定义加载的Geoserver图层
/**
* 添加基础底图数据
*/
public void addBasicLayer() {
WmsLayerConfig config = new WmsLayerConfig();
config.serviceAddress = "http://192.168.0.1:7080/geoserver/ksh/wms";
config.wmsVersion = "1.1.0";
config.layerNames = "ksh:wax2018";
basicLayer = new WmsLayer(new Sector().setFullSphere(), 1, config);
worldWindow.getLayers().addLayer(basicLayer);
}
注意:这里的方法是没错的,但是为了保密,这个图层是不可用的,所以需要自己根据自己发布的图层信息进行填写。
到此,加载Geoserver的操作就完成了。
效果图如下。
以下是完整代码
package com.example.testaddwms;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.FrameLayout;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.globe.BasicElevationCoverage;
import gov.nasa.worldwind.layer.BackgroundLayer;
import gov.nasa.worldwind.layer.RenderableLayer;
import gov.nasa.worldwind.ogc.WmsLayer;
import gov.nasa.worldwind.ogc.WmsLayerConfig;
public class MainActivity extends AppCompatActivity {
private WorldWindowFragment worldWindowFragment;
private WmsLayer basicLayer;
private WorldWindow worldWindow;
private RenderableLayer locateLayer;
FrameLayout globelayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WorldWindow worldWindowView = createWorldWindow();
globelayout=findViewById(R.id.globe);
globelayout.addView(worldWindowView);
}
public WorldWindow createWorldWindow() {
worldWindow = new WorldWindow(getApplicationContext());
worldWindow.getLayers().addLayer(new BackgroundLayer());
worldWindow.getGlobe().getElevationModel().addCoverage(new BasicElevationCoverage());
worldWindow.getNavigator().setLatitude(38.876828);
worldWindow.getNavigator().setLongitude(116.458833);
worldWindow.getNavigator().setAltitude(2000);
addBasicLayer();
locateLayer = new RenderableLayer("VectorTest");
worldWindow.getLayers().addLayer(locateLayer);
return worldWindow;
}
/**
* 添加基础底图数据
*/
public void addBasicLayer() {
WmsLayerConfig config = new WmsLayerConfig();
config.serviceAddress = "http://192.168.0.0:7080/geoserver/ksh/wms";
config.wmsVersion = "1.1.0";
config.layerNames = "ksh:wax2018";
basicLayer = new WmsLayer(new Sector().setFullSphere(), 1, config);
worldWindow.getLayers().addLayer(basicLayer);
}
}