ArcGIS Runtime SDK for Android 100.3.0加载离线.slpk文件

1
分享 2018-08-09
  • 需求:

        采用ArcGIS Runtime SDK for Android 100.3.0加载离线slpk文件。关于slpk的介绍,请参考ArcGIS官方介绍http://pro.arcgis.com/zh-cn/pro-app/help/sharing/overview/scene-layer-package.htm
  • 效果图:

ArcGIS Pro中加载slpk文件
1.png

Android真机中加载slpk文件 
2.png

  • 代码实现:

(1)MainActivity.java
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.esri.arcgisruntime.layers.ArcGISSceneLayer;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.SceneView;

import java.io.File;

/**
* 加载slpk
*/
public class MainActivity1 extends AppCompatActivity {

private SceneView mSceneView;
private ArcGISScene scene;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// create a scene and add a basemap to it
scene = new ArcGISScene();
// scene.setBasemap(Basemap.createImagery());

mSceneView = (SceneView) findViewById(R.id.sceneView);
mSceneView.setScene(scene);

boolean isSdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 判断sdcard是否存在
if (isSdCardExist) {
String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
Log.d("path", sdpath.toString());

//获取tpk文件在手机sdcard中的存放路径
String filepath = sdpath + File.separator + "Buildings.slpk";
if (!filepath.isEmpty()) {

// add a scene service to the scene for viewing buildings
ArcGISSceneLayer sceneLayer = new ArcGISSceneLayer(filepath);
scene.getOperationalLayers().add(sceneLayer);

// add a camera and initial camera position
Camera camera = new Camera(39.915559, 116.403811, 50, 345, 65, 0);
mSceneView.setViewpointCamera(camera);
}
} else {
Toast.makeText(MainActivity1.this, "sdcard is not exit", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onPause() {
super.onPause();
mSceneView.pause();
}

@Override
protected void onResume() {
super.onResume();
mSceneView.resume();
}

@Override
protected void onDestroy() {
super.onDestroy();
mSceneView.dispose();
}
}
        代码中通过创建SceneView实例对象,同时创建一个ArcGISScene实例,并将ArcGISScene实例给SceneView实例对象的setScene()方法,作为其参数;判断是否存在Sdcard,如果存在则获取Sdcard根目录路径,并将获取的slpk文件路径传给ArcGISSceneLayer构造函数;最终通过ArcGISScene实例对象的getOperationalLayers().add(sceneLayer)加载slpk文件。
        此外,要注意Camera构造函数中参数的设置,Camera类代表了一个不变的对象,拥有位置、方向和倾斜角度。
(2)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

<!-- SceneView -->
<com.esri.arcgisruntime.mapping.view.SceneView
android:id="@+id/sceneView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"></com.esri.arcgisruntime.mapping.view.SceneView>
</android.support.constraint.ConstraintLayout>
(3)AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
        slpk文件存放,可以通过Android Studio编译器的Device File Explorer进行上传,下图是将slpk文件上传到手机sdcard根目录下:
3.png

        代码中获取的sdcard根目录路径是storage/emulated/0,关于这个根目录到底在手机中什么位置,可以参考https://blog.csdn.net/gislaozhang/article/details/81318520 这篇博文。
 
文章来源:https://blog.csdn.net/gislaozhang/article/details/81541825

0 个评论

要回复文章请先登录注册