首页 / 浏览问题 / 组件GIS / 问题详情
iobjectjava 添加图层不成功
209EXP 2021年12月14日

通过以下程序执行之后添加图层不成功,程序执行不报错,执行完成之后,目标文件中地图选项中没有添加的图层数据,其中

RiskSuperMapConst.m_datasetPoint的值为“Capital”

,麻烦老师帮忙看一下,  代码如下:

package com.example.LayerAdding_2_2;

import com.example.commom.RiskConst.RiskSuperMapConst;
import com.supermap.data.*;
import com.supermap.mapping.Layer;
import com.supermap.mapping.LayerSettingVector;
import com.supermap.ui.Action;
import com.supermap.ui.MapControl;

import java.awt.*;


public class LayerAddingTest {

    public static void main(String[] args) {
        LayerAddingTest layerAdding =new LayerAddingTest();
        layerAdding.addPoint(false);
    }

    private Workspace m_workspace;
    private DatasourceConnectionInfo m_dataCon;
    /*map控件*/
    private MapControl m_mapControl;

    private Datasource m_datasource;

    public LayerAddingTest() {
//        initialize();
        initializeTwo();
    }

    private void initialize(){
        m_workspace = new Workspace();
        m_dataCon = new DatasourceConnectionInfo();
        m_mapControl = new MapControl();
        m_mapControl.getMap().setWorkspace(m_workspace);

        // 调整mapControl的状态
//        m_mapControl.setAction(Action.PAN);
//        m_mapControl.getMap().viewEntire();
    }
    private void initializeTwo() {
        try {
            m_workspace = new Workspace();
            m_dataCon = new DatasourceConnectionInfo();
            m_mapControl = new MapControl();
            m_mapControl.getMap().setWorkspace(m_workspace);

            // 打开工作空间及地图
            /*WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(
                    "../../SampleData/World/World.smwu");*/

            WorkspaceConnectionInfo conInfo = new WorkspaceConnectionInfo(
                    "D:\\supermap\\软件安装\\supermap-iobjectsjava-9.0.0-15320-63946-linux64-tar.gz-chs\\SampleData\\World\\World.smwu");


            conInfo.setType(WorkspaceType.SMWU);
            m_workspace.open(conInfo);
            m_datasource = m_workspace.getDatasources().get(0);

            // 调整mapControl的状态
            m_mapControl.setAction(Action.PAN);
            m_mapControl.getMap().viewEntire();
boolean result = m_workspace.getMaps().setMapXML(m_mapControl.getMap().toXML(),"AA");
System.out.println("===>"+result);
m_workspace.save();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    /**
     * 向地图中添加点数据集:
     * 备注:(success)
     */
    public void addPoint(boolean m_isCustomStyle) {
        try {

//            Datasource m_datasource = MapDataSourceUtils.connectPostgresDataSource(m_workspace,m_dataCon);
            /*获取点数据集*/
            int count = m_datasource.getDatasets().getCount();
            for(int i=0;i<count;i++){
                System.out.println(m_datasource.getDatasets().get(i).getName());
            }
            DatasetVector dataset = (DatasetVector) m_datasource.getDatasets()
                    .get(RiskSuperMapConst.m_datasetPoint);

            //设置风格并添加数据集
            Layer layer = null;
            /*是否自定义风格的标志,true:自定义风格*/
            if (m_isCustomStyle) {
                LayerSettingVector setting = new LayerSettingVector();
                setting.getStyle().setLineColor(Color.GREEN);
                setting.getStyle().setMarkerSize(new Size2D(4, 4));
                setting.getStyle().setMarkerSymbolID(12);
                layer = m_mapControl.getMap().getLayers().add(dataset, setting,
                        true);
            } else {
                layer = m_mapControl.getMap().getLayers().add(dataset, true);
            }

            //全幅显示添加的图层
            m_mapControl.getMap().ensureVisible(layer);
            m_mapControl.getMap().refresh();
boolean result = m_workspace.getMaps().setMapXML(m_mapControl.getMap().toXML(),"AA");
System.out.println("===>"+result);
m_workspace.save();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

1个回答

您好,如果向地图中添加图层,建议您把layer添加到地图的Layers集合中。具体方法您可以查看帮助文档API中Layers的相关示例。

4,151EXP 2021年12月14日
上述代码是参照LayerAdding 进行操作的,请问一下,有什么问题吗?
您看一下是否是工作空间没有保存导致的,Workspace.Save();
...