上一篇文章中咱們實現了離線要素的編輯操做,這一篇中主要介紹離在線一體化技術中最後一個環節離線數據的同步功能,經過對數據的上傳,服務器端的版本化管理,實現數據生產管理的整個流程。html
轉載請註明出處:http://www.cnblogs.com/gis-luq/p/5858062.html java
activity_main.xmlandroid
<?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.syncgdb.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" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="離線數據同步" android:id="@+id/btnSync" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>
完整代碼git
package com.example.syncgdb; 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.widget.Button; import android.widget.Toast; 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.geodatabase.GeodatabaseFeatureTableEditErrors; import com.esri.core.map.CallbackListener; import com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback; import com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo; import com.esri.core.tasks.geodatabase.GeodatabaseSyncTask; import com.esri.core.tasks.geodatabase.SyncGeodatabaseParameters; import java.io.File; import java.io.FileNotFoundException; import java.util.Map; public class MainActivity extends AppCompatActivity { protected static final String TAG = "syncGDB"; private Context context; private MapView mMapView;//地圖容器 private static String onlineFeatureLayerUrl = "http://192.168.1.212:6080/arcgis/rest/services/testdata/FeatureServer";//在線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); this.context = this; this.mMapView = (MapView)findViewById(R.id.map); //設置離線地理數據庫存儲路徑 localGdbFilePath = createGeodatabaseFilePath(); //加載離線地理數據庫 addFeatureLayer(localGdbFilePath); mProgressDialog = new ProgressDialog(context); //設置點擊進度對話框外的區域對話框不消失 mProgressDialog.setCanceledOnTouchOutside(false); mProgressDialog.setTitle("正在同步離線地理數據庫副本到服務器"); //綁定按鈕設置下載事件 Button btnSyncGDB = (Button)this.findViewById(R.id.btnSync); btnSyncGDB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SyncOfflineData();//同步離線地理數據庫 } }); } /** * Geodatabase文件存儲路徑 */ static String createGeodatabaseFilePath() { return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "/RuntimeOfflineEdit" + File.separator + "demo.geodatabase"; } /** * 讀取Geodatabase中離線地圖信息 * @param featureLayerPath 離線Geodatabase文件路徑 */ private void addFeatureLayer(String featureLayerPath) { Geodatabase localGdb = null; try { localGdb = new Geodatabase(featureLayerPath); } catch (FileNotFoundException e) { e.printStackTrace(); } // 添加FeatureLayer到MapView中 if (localGdb != null) { for (GeodatabaseFeatureTable gdbFeatureTable : localGdb.getGeodatabaseTables()) { if (gdbFeatureTable.hasGeometry()){ FeatureLayer layer = new FeatureLayer(gdbFeatureTable); mMapView.addLayer(layer); } } } } /** * 同步離線地理數據庫 */ private void SyncOfflineData() { Log.i(TAG, "Sync GeoDatabase"); // create a dialog to update user on progress mProgressDialog.show(); gdbSyncTask = new GeodatabaseSyncTask(onlineFeatureLayerUrl, 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()) { SyncGeodatabase(fsInfo); } } }); } /** * 根據FeatureServiceInfo信息獲取離線地理數據庫同步信息 * @param featureServerInfo 服務參數信息 */ private void SyncGeodatabase(FeatureServiceInfo featureServerInfo) { try { // 建立一個離線地理數據庫 Geodatabase gdb = new Geodatabase(localGdbFilePath); // 獲取離線地理數據庫同步參數 final SyncGeodatabaseParameters syncParams = gdb.getSyncParameters(); CallbackListener<Map<Integer, GeodatabaseFeatureTableEditErrors>> syncResponseCallback = new CallbackListener<Map<Integer, GeodatabaseFeatureTableEditErrors>>() { @Override public void onCallback(Map<Integer, GeodatabaseFeatureTableEditErrors> objs) { mProgressDialog.dismiss(); if (objs != null) { if (objs.size() > 0) { showMakeText("同步完成,可是發生錯誤"); } else { showMakeText("同步完成:同步成功"); } } else { showMakeText("同步完成:同步成功"); } } @Override public void onError(Throwable e) { Log.e(TAG, "", e); mProgressDialog.dismiss(); Toast.makeText(context, "Error:"+e.toString(), Toast.LENGTH_SHORT).show(); } }; GeodatabaseStatusCallback statusCallback = new GeodatabaseStatusCallback() { @Override public void statusUpdated(GeodatabaseStatusInfo status) { final String progress = status.getStatus().toString(); //在UI線程更新下載狀態 ((Activity)context).runOnUiThread(new Runnable(){ @Override public void run() { mProgressDialog.setMessage("數據同步中,請稍後……"); } }); } }; // 執行同步 gdbSyncTask.syncGeodatabase(syncParams, gdb, statusCallback, syncResponseCallback); } catch (Exception e) { e.printStackTrace(); } } /** * 在UI線程中執行狀態提示 * @param msg */ private void showMakeText(final String msg) { //在UI線程更新下載狀態 ((Activity)context).runOnUiThread(new Runnable(){ @Override public void run() { Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } }); } }
源代碼託管地址:http://git.oschina.net/gis-luq/RuntimeOfflineEdit數據庫
《ArcGIS Runtime SDK for Android開發筆記》——離在線一體化技術:概述服務器
《ArcGIS Runtime SDK for Android開發筆記》——離在線一體化技術:離線矢量數據下載app
《ArcGIS Runtime SDK for Android開發筆記》——離在線一體化技術:離線矢量數據編輯ide
《ArcGIS Runtime SDK for Android開發筆記》——離在線一體化技術:離線矢量數據同步post
《ArcGIS Runtime SDK for Android開發筆記》——數據製做篇:發佈具備同步能力的FeatureService服務fetch