安卓智能地图开发与实施十五:离线与同步 - ArcGIS Runtime SDK for Android(Version 100.0.0)

离线与同步(GeodatabaseSyncTask)

针对编辑,上一节中凡事离线的,各种添加、更新、删除等都是针对终端上存储的离线数据。可以在没有网络的环境下完成,当具备网络时,还需同步到ArcGIS Server服务器中对应的要素服务(Feature Service)。

创建GeodatabaseSyncTask

private void createGeodatabaseSyncTask() {
// Create a GeodatabaseSyncTask and add a done loading listener
geodatabaseSyncTask = new GeodatabaseSyncTask(FEATURE_SERVICE_URL);
geodatabaseSyncTask.addDoneLoadingListener(new Runnable() {
@Override public void run() {
// Check if it failed to load. If its not in the failed state, inside a done loading listener the only other
// valid state is loaded
if (geodatabaseSyncTask.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
dealWithException(geodatabaseSyncTask.getLoadError());
}
else
{
// Now the sync task is loaded, use it to create GenerateGeodatabaseParameters, or sync changes
generateGeodatabaseParameters();
}
}
});
// Load the sync task
geodatabaseSyncTask.loadAsync();
}

设置离线参数

指定范围、只保留第一个图层。

private void generateGeodatabaseParameters() {
// Create GenerateGeodatabaseParameters from the GeodatabaseSyncTask using current map extent
Envelope generateExtent = mapView.getVisibleArea().getExtent();
final ListenableFuture<GenerateGeodatabaseParameters> generateParamsFuture =
geodatabaseSyncTask.createDefaultGenerateGeodatabaseParametersAsync(generateExtent);
generateParamsFuture.addDoneListener(new Runnable() {
@Override
public void run() {
try {
generateParams = generateParamsFuture.get();
// Change GenerateGeodatabaseParameters as required - e.g. removes all layers except first from the sync
for (int i = generateParams.getLayerOptions().size() -1; i >= 1; i--) {
generateParams.getLayerOptions().remove(i);
}
// Now the parameters are prepared, use them with the sync task to generate a geodatabase
generateGeodatabase();
}
catch (InterruptedException | ExecutionException e) {
dealWithException(e);
}
}
});
}

获取离线数据

private void generateGeodatabase() {
// Download a copy of the geodatabase to the specified path, using prepared parameters
generateJob = geodatabaseSyncTask.generateGeodatabaseAsync(generateParams, GDB_PATH);
// Add listener to check and report on the current download status
generateJob.addJobChangedListener(new Runnable() {
@Override
public void run() {
// Deal with any errors found while generating the geodatabase
if (generateJob.getError() != null) {
dealWithException(generateJob.getError());
}
else {
// While job is in progress, review job messages or update progress in logs or user interface...
updateUiWithJobProgress(generateJob);
}
}
});
// Add listener to deal with the completed job
generateJob.addJobDoneListener(new Runnable() {
@Override
public void run() {
// Check the job state is complete, deal with any errors
if ((generateJob.getStatus() != Job.Status.SUCCEEDED) || (generateJob.getError() != null)) {
dealWithException(generateJob.getError());
}
else {
// Get the completed Geodatabase from the job
if (generateJob.getResult() instanceof Geodatabase) {
Geodatabase syncResultGdb = (Geodatabase) generateJob.getResult();
geodatabase = syncResultGdb;
// Use the geodatabase, for example, load it and create FeatureLayers based on the tables it contains
loadGeodatabase();
}
}
}
});
// Start the Job to generate the geodatabase
generateJob.start();
}

设置同步参数

private void createSyncParameters() {
// Create SyncGeodatabaseParameters based on default parameters from the sync tasks
final ListenableFuture<SyncGeodatabaseParameters> syncParamsFuture = geodatabaseSyncTask
.createDefaultSyncGeodatabaseParametersAsync(geodatabase);
syncParamsFuture.addDoneListener(new Runnable() {
@Override
public void run() {
try
{
// Retrieve the SyncGeodatabaseParameters from the future
syncParams = syncParamsFuture.get();
// Continue on to sync the database using the parameters
syncGeodatabase();
}
catch (InterruptedException | ExecutionException e) {
dealWithException(e);
}
}
});
}

同步离线编辑

private void syncGeodatabase() {
// Start the sync operation on the geodatabase
syncJob = geodatabaseSyncTask.syncGeodatabaseAsync(syncParams, geodatabase);
// Add listener to check and report on the current sync status
syncJob.addJobChangedListener(new Runnable() {
@Override
public void run() {
// Deal with any errors found while syncing the geodatabase
if (syncJob.getError() != null) {
dealWithException(syncJob.getError());
}
else {
// While job is in progress, review job messages or update progress in logs or user interface...
updateUiWithJobProgress(syncJob);
}
}
});
// Add listener to deal with the completed job
syncJob.addJobDoneListener(new Runnable() {
@Override
public void run() {
// Check the job state is complete, deal with any errors
if ((syncJob.getStatus() != Job.Status.SUCCEEDED) || (syncJob.getError() != null)) {
dealWithException(syncJob.getError());
}
else {
// Get the SyncLayerResults returned from the sync
List<SyncLayerResult> syncResults = (List<SyncLayerResult>) syncJob.getResult();
if (syncResults != null) {
// Check sync results, for example update the UI to inform the user
dealWithSyncResults(syncResults);
}
}
}
});
// Start the Job to sync the geodatabase
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾神说D

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值