过程参考 数据制作篇:发布具有同步能力的FeatureService服务 一文。

转载请注明出处:http://www.cnblogs.com/gis-luq/p/5858048.html
ArcGIS Runtime SDK 配置实现过程略:具体请参考:
基于Android Studio构建ArcGIS Android开发环境
基于Android Studio构建ArcGIS Android开发环境(离线部署)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.downgdb.MainActivity">
<!-- MapView -->
<com.esri.android.map.MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
mapoptions.MapType="Topo"
mapoptions.ZoomLevel="5"
mapoptions.center="28.671298, 104.066404" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="@+id/linearLayout"
android:background="@color/primary_material_light">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editTextGDBUrl"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_weight="1"
android:text="http://192.168.1.212:6080/arcgis/rest/services/testdata/FeatureServer"
android:inputType="textUri" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数据下载"
android:id="@+id/btnDownGDB" />
</LinearLayout>
</RelativeLayout>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
GeodatabaseSyncTask类,实现下载同步功能

本次示例代码主要用到以下三个参数:
package com.example.downgdb;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import com.esri.android.map.FeatureLayer;
import com.esri.android.map.MapView;
import com.esri.core.ags.FeatureServiceInfo;
import com.esri.core.geodatabase.Geodatabase;
import com.esri.core.geodatabase.GeodatabaseFeatureTable;
import com.esri.core.map.CallbackListener;
import com.esri.core.tasks.geodatabase.GenerateGeodatabaseParameters;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo;
import com.esri.core.tasks.geodatabase.GeodatabaseSyncTask;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
protected static final String TAG = "downGDB";
private Context context;
private MapView mMapView;//地图容器
private EditText editTextDownGDBUrl;//GDB地址
private Button btnDownGDB;//下载GDB
private static String onlineFeatureLayerUrl;//在线FeatureLayer地址
private static String localGdbFilePath;//离线GDB地址
private GeodatabaseSyncTask gdbSyncTask;//离线地理数据库下载Task
private ProgressDialog mProgressDialog;//状态框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
// 默认软键盘不弹出
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
this.mMapView = (MapView)findViewById(R.id.map);
this.editTextDownGDBUrl = (EditText)findViewById(R.id.editTextGDBUrl);
//获取并设置在线服务地址
this.onlineFeatureLayerUrl = this.editTextDownGDBUrl.getText().toString();
mProgressDialog = new ProgressDialog(context);
//设置点击进度对话框外的区域对话框不消失
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setTitle("正在创建离线地理数据库副本");
//绑定按钮设置下载事件
btnDownGDB = (Button)this.findViewById(R.id.btnDownGDB);
btnDownGDB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadData(onlineFeatureLayerUrl);//下载离线数据
}
});
}
/**
* Geodatabase文件存储路径
*/
static String createGeodatabaseFilePath() {
return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "/RuntimeOfflineEdit"
+ File.separator + "demo.geodatabase";
}
/**
* 下载离线地理数据库
* @param url FeatureService服务地址
* 例如:http://192.168.1.212:6080/arcgis/rest/services/testdata/FeatureServer
* 支持ArcGIS for Server 10.2.1以上版本,必须开启FeatureServer要素同步功能
*/
private void downloadData(String url) {
Log.i(TAG, "Create GeoDatabase");
// create a dialog to update user on progress
mProgressDialog.show();
gdbSyncTask = new GeodatabaseSyncTask(url, null);
gdbSyncTask.fetchFeatureServiceInfo(new CallbackListener<FeatureServiceInfo>() {
@Override
public void onError(Throwable arg0) {
Log.e(TAG, "获取FeatureServiceInfo失败");
}
@Override
public void onCallback(FeatureServiceInfo fsInfo) {
if (fsInfo.isSyncEnabled()) {
createGeodatabase(fsInfo);
}
}
});
}
/**
* 根据FeatureServiceInfo信息创建离线地理数据库文件
* @param featureServerInfo 服务参数信息
*/
private void createGeodatabase(FeatureServiceInfo featureServerInfo) {
// 生成一个geodatabase设置参数
GenerateGeodatabaseParameters params = new GenerateGeodatabaseParameters(
featureServerInfo, mMapView.getMaxExtent(), mMapView.getSpatialReference());
// 下载结果回调函数
CallbackListener<String> gdbResponseCallback = new CallbackListener<String>() {
@Override
public void onError(final Throwable e) {
Log.e(TAG, "创建geodatabase失败");
mProgressDialog.dismiss();
}
@Override
public void onCallback(String path) {
Log.i(TAG, "Geodatabase 路径: " + path);
mProgressDialog.dismiss();
loadGeodatabase(path);
}
};
// 下载状态回调函数
GeodatabaseStatusCallback statusCallback = new GeodatabaseStatusCallback() {
@Override
public void statusUpdated(final GeodatabaseStatusInfo status) {
final String progress = status.getStatus().toString();
//在UI线程更新下载状态
((Activity)context).runOnUiThread(new Runnable(){
@Override
public void run() {
mProgressDialog.setMessage("数据下载中,请稍后……");
}
});
}
};
//设置离线地理数据库存储路径
localGdbFilePath = createGeodatabaseFilePath();
//执行下载Geodatabase数据库
gdbSyncTask.generateGeodatabase(params, localGdbFilePath, false, statusCallback, gdbResponseCallback);
}
/**
* 加载离线地理数据库
* @param path .geodatabse文件路径
*/
private void loadGeodatabase(String path) {
// 创建一个geodatabase数据库
Geodatabase localGdb = null;
try {
localGdb = new Geodatabase(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 添加FeatureLayer到MapView中
if (localGdb != null) {
for (GeodatabaseFeatureTable gdbFeatureTable : localGdb.getGeodatabaseTables()) {
if (gdbFeatureTable.hasGeometry()){
mMapView.addLayer(new FeatureLayer(gdbFeatureTable));
}
}
}
}
}

源代码托管地址:http://git.oschina.net/gis-luq/RuntimeOfflineEdit
http://blog.csdn.net/arcgis_all/article/details/20442663
《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述
《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据下载
《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据编辑
《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据同步
《ArcGIS Runtime SDK for Android开发笔记》——数据制作篇:发布具有同步能力的FeatureService服务
《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据下载
原文:http://www.cnblogs.com/love540376/p/6434457.html