超图 移动端 imobile 三维快速入门
IMobile for Android +IServer 在线服务三维服务快速入门
本文将使用超图提供的组件进行移动端三维场景的基础入门,数据为通过Iserver发布的三维服务,因此本文分为两部分:
- 通过IServer发布三维服务
- 使用IMobile开发组件 打开场景
发布三维服务:
*博主使用的数据非超图示例的数据,许可到期了,没办法截不了图
- 在超图桌面端打开包含所需数据集的数据源,并将其添加到新的球面场景中
- 正确添加后,关闭场景,选择保存场景,名称自定义(这里命名为scene)
- 在保存的场景scene上右键,生成场景缓存,因为展示的目标平台为Android,所以,需要把下拉框中的普通PC改成Android相关的选项,其他默认即可
- 生成成功后,把scene中的图层全部移除,然后在普通图层上右键,添加影像缓存,把生成的scp文件添加进去,保存场景,再保存工作空间,关闭并保存工作空间(这里命名为scene_ws)(最好关闭,防止被占用)
- 启动IServer
- 找到快速发布服务,选择类型为工作空间,远程浏览文件,选择scene_ws,然后勾选三维服务(后续需要查询属性信息可以把数据服务的勾打上),最后发布
- 发布成功后,可以在浏览器中打开三维服务的连接,(xxx为你的三维服务名称)。
- 记住自己的三维服务场景地址,具体不详细说,博主的场景地址为:(加黑的位置需要改成自己的,我的三维服务名称为3D-android,Iserver运行的主机IP是:192.168.0.111,端口:8090)
http://192.168.0.111:8090/iserver/services/3D-android/rest/realspace
- 场景名称也需要用到,我的名称为scene
- 到此,三维服务发布完毕
使用IMobile组件二次开发
*开发环境搭建省略。。。
- 为我们的app编写主界面,如下(以下双斜杠只是为了表达注释,实际代码中不能这样使用)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
//加入三维场景显示的控件
<com.supermap.realspace.SceneView
android:id="@+id/sceneView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.supermap.realspace.SceneView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="5px"
android:orientation="vertical">
//场景地址输入框
<EditText
android:id="@+id/s_url"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="@color/white"
android:text="@string/scene_url" />
//场景名称输入框
<EditText
android:id="@+id/sn"
android:layout_width="match_parent"
android:textColor="@color/white"
android:layout_height="60dp"
android:text="@string/scene_name" />
//打开场景按钮
<Button
android:id="@+id/btnOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="open" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
2. 编写后台代码,在MainActivity中
super.onCreate(savedInstanceState);
//注意:这里需要申请读取存储等相关权限,如果没有这些权限,Environment.initialization(this)这句话可能会抛出 错误路径 或者空数组相关异常,
//requestPermissions();自己实现
//获取运行环境的绝对路径
String rootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
//设置许可路径,注意Environment是超图的接口, StringKey.LICENSE_REA_PATH为许可存放目录,下文会说到
Environment.setLicensePath(rootPath + StringKey.LICENSE_REA_PATH);
Environment.initialization(this);
setContentView(R.layout.activity_main);
//开启全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//强制横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//绑定控件,需要声明全局字段,这里省去声明
sceneView = findViewById(R.id.sceneView);
sceneControl = sceneView.getSceneControl();
btnOpen = findViewById(R.id.btnOpen);
editText = findViewById(R.id.s_url);
sceneName = findViewById(R.id.sn);
//注册打开场景按钮点击事件
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("opening the scene... url:" + editText.getText().toString() + " scene:" + sceneName.getText().toString());
//打开场景
sceneControl.getScene().open(editText.getText().toString(), sceneName.getText().toString());
sceneControl.getScene().refresh();
Layer3Ds lys = sceneControl.getScene().getLayers();
for (int i = 0; i < lys.getCount(); i++) {
//打印图层名称
System.out.println("ly: "+lys.get(i).getName());
}
//飞行定位到指定经纬度
sceneControl.getScene().flyToPoint(new Point3D(102.49257820, 25.73540740, 500), 5);
}
});
3. 解析一下许可配置
首先得去超图官网申请一个许可文件,然后把这个许可文件拷贝到测试机(SDCard根目录下创建supmap文件夹,再其下创建lic文件夹,把许可复制到lic中)中,如下图
Android Studio中自带Device File Explore (设备文件浏览器),可以直接在上面右键upload把许可文件上传到设备中
这样,许可的相对位置就是:/supermap/lic/,也就是StringKey.LICENSE_REA_PATH=“/supermap/lic/”
编译。。安装结果如下(界面有点丑)
*android设备已插入USB 但是识别不到?请看我的博客:https://blog.****.net/WPR13005655989/article/details/80560216
*申请权限可以使用EasyPermissions框架