supermap 开发|-Supermap iobjects入门案列JAVA - showMessageDialog(null, "
1.必要环境java jdk1.8(我的)
2.下载依赖,我看官网说能maven 但我没试 我直接下的包
下载这个
3.创建项目(这个大家应该都会,就不写了),导入依赖
添加这个BIN
3.下载SuperMap IDesktopX(后面会用到地图)
安装后是这样的(一定要记得你的安装路径,我安装的时候他没给我创建图标,然后你在安装目录下会找到个startup,点击他就能运行)
4.记得去官网注册一下,要不然跑步起来
软件商店 地址在这
5.代码部分
import com.supermap.data.*;
import com.supermap.mapping.*;
import com.supermap.ui.*;
import com.supermap.ui.Action;
import javax.swing.*;
import java.awt.*;
public class SimpleGISApp extends JFrame {
private MapControl mapControl; // 地图控件
private Workspace workspace; // 工作空间对象
private Map map; // 地图对象
private JToolBar toolBar; // 工具栏
public SimpleGISApp() {
// 设置窗口属性
setTitle("简单GIS应用");
setSize(1024, 768);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 初始化工作空间
workspace = new Workspace();
// 创建地图控件
mapControl = new MapControl();
map = mapControl.getMap();
// 创建工具栏
createToolBar();
// 设置主窗口布局
setLayout(new BorderLayout());
add(toolBar, BorderLayout.NORTH); // 工具栏放在顶部
add(mapControl, BorderLayout.CENTER); // 地图控件放在中心
// 初始化地图
initializeMap();
}
private void createToolBar() {
toolBar = new JToolBar(); // 创建工具栏
toolBar.setFloatable(false); // 工具栏不可浮动
// 创建并添加"放大"按钮
JButton btnZoomIn = new JButton("放大");
btnZoomIn.addActionListener(e ->mapControl.setAction(Action.ZOOMIN));
toolBar.add(btnZoomIn);
// 创建并添加"缩小"按钮
JButton btnZoomOut = new JButton("缩小");
btnZoomOut.addActionListener(e ->mapControl.setAction(Action.ZOOMOUT));
toolBar.add(btnZoomOut);
// 创建并添加"平移"按钮
JButton btnPan = new JButton("平移");
btnPan.addActionListener(e ->mapControl.setAction(Action.PAN));
toolBar.add(btnPan);
// 创建并添加"选择"按钮
JButton btnSelect = new JButton("选择");
btnSelect.addActionListener(e ->mapControl.setAction(Action.SELECT));
toolBar.add(btnSelect);
}
private void initializeMap() {
try {
// 创建并配置工作空间连接信息
WorkspaceConnectionInfo connectionInfo = new WorkspaceConnectionInfo();
connectionInfo.setType(WorkspaceType.SMW); // 设置为SuperMap工作空间类型
connectionInfo.setServer("D:/surdata/China1.smwu"); // 设置工作空间文件路径
workspace.open(connectionInfo); // 打开工作空间
map.setWorkspace(workspace); // 设置地图的工作空间
// 获取地图数量,选择第4个地图(索引3)
int mapCount = workspace.getMaps().getCount();
if (mapCount > 0) {
String mapName = workspace.getMaps().get(3); // 获取地图名称
boolean openResult = map.open(mapName); // 打开地图
// 遍历所有图层,确保全部可见
Layers layers = map.getLayers();
for (int i = 0; i < layers.getCount(); i++) {
Layer layer = layers.get(i);
layer.setVisible(true); // 设置图层可见
}
map.viewEntire(); // 全图显示
map.refresh(); // 刷新地图
mapControl.repaint(); // 重绘控件
mapControl.updateUI(); // 更新控件UI
// 检查所有数据源,若未打开则尝试用实际路径重新打开
Datasources datasources = workspace.getDatasources();
for (int i = 0; i < datasources.getCount(); i++) {
Datasource ds = datasources.get(i);
if (!ds.isOpened()) {
ds.close(); // 关闭原数据源
// 新建连接信息,设置新路径
DatasourceConnectionInfo newInfo = new DatasourceConnectionInfo();
String filename = new java.io.File(ds.getConnectionInfo().getServer()).getName();
String newPath = "D:/soft/surmap/sampleData/WebMap/China100/" + filename;
newInfo.setEngineType(ds.getConnectionInfo().getEngineType());
newInfo.setAlias(ds.getAlias());
newInfo.setServer(newPath);
// 重新打开数据源
datasources.open(newInfo);
}
}
// 再次刷新地图,确保显示
map.refresh();
mapControl.repaint();
mapControl.updateUI();
} else {
// 如果没有地图,弹窗提示
JOptionPane.showMessageDialog(this, "工作空间中没有地图,请用SuperMap桌面软件检查smwu文件!");
}
} catch (Exception e) {
// 初始化地图失败时弹窗提示
JOptionPane.showMessageDialog(this, "初始化地图失败:" + e.getMessage());
}
}
public static void main(String[] args) {
// 登录SuperMap云许可
CloudLicense.login("你注册的电话号码", "你的密码");
// 连接云许可模块,获取许可
License lic = new License();
int code = lic.connect(65400); // 试用许可模块ID 65400
try {
// 启动Swing主线程,显示主窗口
SwingUtilities.invokeLater(() ->{ 拼多多帮助力入口
SimpleGISApp app = new SimpleGISApp();
app.setVisible(true);
});
} catch (Exception e) {
// 初始化失败时弹窗提示
JOptionPane.showMessageDialog(null, "初始化失败:" + e.getMessage());
}
}
}
6.运行截图