本文主要讲解如何借助ArcGIS Runtime SDK for Android显示本地Shapefile文件。
实现步骤:
1.创建Android项目
2.添加Runtime SDK依赖
前两步本文省略,初学者可参照 ArcGIS Runtime SDK for Android 入门(1):第一个地图应用程序(二维)
3.添加权限及OpenGL ES支持
<!--联网权限-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--读取外部存储权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
4.设置界面布局
布局XML代码:
<!-- MapView控件 -->
<com.esri.arcgisruntime.mapping.view.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.esri.arcgisruntime.mapping.view.MapView>
5.编写代码:
思路:
(1)请求文件读写权限(Android 6.0及以上版本)。
(2)获取Shapefile文件路径。
(3)通过路径创建ShapefileFeatureTable对象,并对其进行异步加载。
(4)通过ShapefileFeatureTable对象创建FeatureLayer要素图层对象。
(5)将Shapefile的FeatureLayer要素图层添加到MapView地图中的OperationalLayers业务图层。
(6)将MapView缩放至Shapefile范围进行显示。
准备:在Android设备的内置SD卡中创建ArcGIS文件夹,放入含有Shp文件的Shapefile文件夹。
步骤:
(1)变量准备:
// Log.e的标志变量
private final static String TAG = MainActivity.class.getSimpleName();
// MapView控件变量
private MapView mMapView;
(2)onCreate方法中:
// 创建一个在MapView中显示的以街道地图为底图的新地图对象
mMapView = findViewById(R.id.mapView);
ArcGISMap map = new ArcGISMap(Basemap.createStreetsVector());
mMapView.setMap(map);
//请求设备读写权限并加载数据
requestReadPermission();
(3)方法支持:
// 请求设备读写权限并加载数据
private void requestReadPermission() {
// 定义请求权限
String[] reqPermission = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE };
int requestCode = 2;
// 在API23版本以上中,权限需要在运行时进行请求
if (ContextCompat.checkSelfPermission(MainActivity.this,
reqPermission[0]) == PackageManager.PERMISSION_GRANTED) {
// 加载数据
featureLayerShapefile();
} else {
// 请求权限
ActivityCompat.requestPermissions(MainActivity.this, reqPermission, requestCode);
}
}
// 处理权限请求响应,用户选择完权限后响应
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 加载数据
featureLayerShapefile();
} else {
// 报告给用户权限请求被拒绝
Toast.makeText(MainActivity.this, getResources().getString(R.string.read_permission_denied),
Toast.LENGTH_SHORT).show();
}
}
// 加载shapefile
private void featureLayerShapefile() {
// 获取本地shapefile文件
String path = getSDCardPath()+"/ArcGIS/Shapefile/Aurora_CO_shp/Subdivisions.shp";
ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(path);
shapefileFeatureTable.loadAsync();
shapefileFeatureTable.addDoneLoadingListener(() -> {
if (shapefileFeatureTable.getLoadStatus() == LoadStatus.LOADED) {
// 利用shapefile文件创建一个FeatureLayer文件
FeatureLayer shapefileFeatureLayer = new FeatureLayer(shapefileFeatureTable);
// 将shapefile图层添加到业务图层
mMapView.getMap().getOperationalLayers().add(shapefileFeatureLayer);
// 缩放到shapefile范围
mMapView.setViewpointAsync(new Viewpoint(shapefileFeatureLayer.getFullExtent()));
} else {
String error = "Shapefile feature table failed to load: " + shapefileFeatureTable.getLoadError().toString();
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
Log.e(TAG, error);
}
});
}
// 获取SD卡路径
public String getSDCardPath()
{
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
6.运行APP:可以对Shapefile中的地理数据进行简单的浏览
感谢luq老师的指导