在上一章中我们学习了怎么在Activity中加载Geoserver影像,接下来学习怎么在 Fragment中添加影像。
继续上一章的内容。
在MainActivity中创建Fragment方法,创建好后在OnCreate中调用。
/**
* 添加三维球并显示观测点的经纬度与观测高
*/
public void addGlobeFragment() {
//添加一个FragmentTransaction的实例
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
//用add()方法加上Fragment的对象ObservationFragment
worldWindowFragment = new WorldWindowFragment();
transaction.add(R.id.globe, worldWindowFragment);
//baseGlobeFragment=worldWindowFragment;
transaction.commit();
}
之后新建一个WorldWindowFragment类继承Fragment类
public class WorldWindowFragment extends Fragment {
private WorldWindow worldWindow;
private WmsLayer wenanLayer;
private RenderableLayer locateLayer;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @NonNull Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_worldwind, null);
WorldWindow worldWindowView = createWorldWindow();
worldWindowView.setWorldWindowController(new BasicWorldWindowController());
FrameLayout globelayout=view.findViewById(R.id.globelayout);
globelayout.addView(worldWindowView);
return view;
}
public WorldWindow createWorldWindow() {
worldWindow = new WorldWindow(getContext());
worldWindow.getLayers().addLayer(new BackgroundLayer());
worldWindow.getGlobe().getElevationModel().addCoverage(new BasicElevationCoverage());
addWenanLayer();
locateLayer = new RenderableLayer("VectorTest");
worldWindow.getLayers().addLayer(locateLayer);
return worldWindow;
}
/**
* 添加基础底图数据
*/
public void addWenanLayer(){
WmsLayerConfig config = new WmsLayerConfig();
config.serviceAddress = "http://192.168.0.1:7080/geoserver/ksh/wms";
config.wmsVersion = "1.1.0";
config.layerNames = "ksh:wax2018";
wenanLayer = new WmsLayer(new Sector().setFullSphere(), 1, config);
worldWindow.getLayers().addLayer(wenanLayer);
}
}
fragment_worldwind布局为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--三维球-->
<FrameLayout
android:id="@+id/globelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
至此,开发完全结束。