SuperMap iMobile开发入门(iMobile 10i + AS3.6)

  • 超图防坑指南
  • 文章概要
  • AS3.6安装、SuperMap iMobile 10i下载
  • HelloMap工程创建

文章概要

  1. AS3.6安装、SuperMap iMobile 10i下载 ,一笔带过;
  2. HelloMap工程创建 ,必要步骤记录;
  3. HelloMap示例代码 ,贴代码 + 部分讲解;
  4. HelloMap完整工程下载 ,提供下载链接(GitHub + CSDN);

HelloMap工程创建

  1. 创建工程
    常规操作
  2. 导入jar包
    这里导入data和mapping这两个包就可以了
  3. 导入.so文件
    直接复制了整个文件夹(armeabi-v7a)过来
  4. 配置activity.xml
    添加MapView控件
  5. 配置activity.java
    初始化环境、打开工作空间、关联地图控件和工作空间、权限适配
  6. 配置Manifest.xml
    添加网络权限、读写文件权限等
  7. 配置build.gradle
    添加ndk和sourceSets节点,详细实例代码见下节
    划重点:超图官方教程未进行ndk节点设置,这里不设置会报错的,亲测!
  8. 数据导入手机
    直接使用USB连接手机后,把示例数据和License文件导入手机根目录就行

HelloMap示例代码

build.gradle

// buid.gradle的部分关键代码
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.3"

    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 28
        ...

        ndk {
            abiFilters 'armeabi-v7a'
        }

    }

    buildTypes {
            ...
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
        ...
    implementation files('libs/com.supermap.data_v1001.jar')
    implementation files('libs/com.supermap.mapping_v1001.jar')
}

activity.xml

// activity_main的全部代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.supermap.mapping.MapView
            android:id="@+id/mapview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </com.supermap.mapping.MapView>
    </RelativeLayout>>

</androidx.constraintlayout.widget.ConstraintLayout>

activity.java

// MainActivity的全部代码
package com.bestyou.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import com.supermap.data.Environment;
import com.supermap.data.Workspace;
import com.supermap.data.WorkspaceConnectionInfo;
import com.supermap.data.WorkspaceType;
import com.supermap.mapping.MapControl;
import com.supermap.mapping.MapView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        checkPermission();

        //初始化环境
        Environment.setLicensePath("/sdcard/SuperMap/license/");
        Environment.setTemporaryPath("/sdcard/SuperMap/temp/");
        Environment.setWebCacheDirectory("/sdcard/SuperMap/WebCatch");
        Environment.initialization(this);

        setContentView(R.layout.activity_main);

        //打开工作空间
        Workspace workspace = new Workspace();
        WorkspaceConnectionInfo info = new WorkspaceConnectionInfo();
        info.setServer("/sdcard/SuperMap/GeometryInfo/World.smwu");
        info.setType(WorkspaceType.SMWU);
        workspace.open(info);

        //将地图显示控件和工作空间关联
        MapView mapView = findViewById(R.id.mapview);
        MapControl mapcontrol = mapView.getMapControl();
        mapcontrol.getMap().setWorkspace(workspace);

        //打开工作空间中的第1幅地图
        String mapName = workspace.getMaps().get(0);
        mapcontrol.getMap().open(mapName);
        mapcontrol.getMap().refresh();
    }

    //动态权限适配
    public void checkPermission() {
        boolean isGranted = true;
        if (android.os.Build.VERSION.SDK_INT >= 23) {
            if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                //如果没有写sd卡权限
                isGranted = false;
            }
            if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                isGranted = false;
            }
            Log.i("cbs","isGranted == "+isGranted);
            if (!isGranted) {
                this.requestPermissions(
                        new String[]{
                                Manifest.permission.READ_PHONE_STATE,
                                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                                Manifest.permission.ACCESS_COARSE_LOCATION,
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        102
                );
            }
        }
    }
    
}

Manifest.xml

<-- AndroidManifest的全部代码 -->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.bestyou.myapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission
        android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
        tools:ignore="ProtectedPermissions" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
        <activity android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name">
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
        </activity>
        
    </application>
    
    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true">
    </supports-screens>
    
</manifest>