首页 / 浏览问题 / 移动GIS / 问题详情
添加多个图层
23EXP 2023年08月18日

如下代码是否可以实现将多个天地图服务添加到一个地图中,并在后期进行图层的编辑或显示?

Workspace workspace = new Workspace();
MapView mapView = findViewById(R.id.mapview);
mapControl =mapView.getMapControl();
//省略一部分代码
mapControl.getMap().setWorkspace(workspace);
Datasource datasource1 = workspace.getDatasources().open(info1);  //矢量底图
Datasource datasource2 = workspace.getDatasources().open(info2);  //地形底图
Dataset dataset1 = datasource1.getDatasets().get(0);
Dataset dataset2 = datasource2.getDatasets().get(0);
layers.add(dataset1, true);
layers.add(dataset2, true);

如果上面是错误的,应该怎么写呢,另外,怎么将数据源打开添加到图层中,其索引是根据layers.add的顺序生成的吗

运行后报错:
 

FATAL EXCEPTION: main
Process: com.example.tianditu, PID: 15258
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tianditu/com.example.tianditu.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.supermap.mapping.Layer com.supermap.mapping.Layers.add(com.supermap.data.Dataset, boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3771)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3938)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2318)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8306)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.supermap.mapping.Layer com.supermap.mapping.Layers.add(com.supermap.data.Dataset, boolean)' on a null object reference
at com.example.tianditu.MainActivity.onemapview(MainActivity.java:85)
at com.example.tianditu.MainActivity.onCreate(MainActivity.java:56)
at android.app.Activity.performCreate(Activity.java:8194)
at android.app.Activity.performCreate(Activity.java:8160)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3744)

1个回答

您好,根据以上信息来看,注意“com.example.tianditu.MainActivity.onemapview(MainActivity.java:85)”位置的代码,提示'com.supermap.mapping.Layer com.supermap.mapping.Layers.add(com.supermap.data.Dataset, boolean)',应该检查对应的数据源是否打开失败,打开后数据源为空,获取其中数据集管理类导致抛出异常
3,750EXP 2023年08月21日
...