在ArcGIS for Android10.2中提供了一个离线矢量的例子(Offline Editor (BETA)),这个例子除了可以学习Geodatabase的下载制作,编辑,同步等矢量离线的方法,还有其他的技术也涉及到了,例如tpk的使用,popup的使用,还有点线面的绘制和修改等。这个例子比较复杂,看着头大,所以决定自己手动敲一遍。
1. 去http://www.arcgis.com/home/item.html?id=9a7e015149854c35b5eb94494f4aeb89下载好tpk数据,放到sd卡下的/ArcGIS/samples/OfflineEditor/目录中。
2.新建一个ArcGIS for Android项目,命名OfflineEditor,生成一个OfflineEditorActivity,在layout文件夹下敲一遍例子中的布局文件中的offliner.xml、offlinerdownloadlayout.xml、offlinereditlayout.xml、offlinersynclayout.xml这四个布局文件,并把OfflineEditorActivity的布局设置成offliner.xml。
3. 复制drawable中的图标,敲下其中的xml代码。
4. 在OfflineEditorActivity中敲入与布局文件定义的按钮点击事件的空方法。最后OfflineEditorActivity中代码如下:
public class OfflineEditorActivity extends Activity {
MapView mMapView;
ToggleButton tbtn_switch;//切换底图按钮
Button btn_edit;//编辑按钮
Button btn_remove;//删除节点按钮
Button btn_cancel;//取消按钮
Button btn_clear;//清除按钮
Button btn_undo;//撤销按钮
Button btn_save;//保存按钮
TabHost tabhost;//TabHost
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置Activity带有圆形的进度
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(false);
setContentView(R.layout.offliner);
mMapView = (MapView) findViewById(R.id.map);
tbtn_switch=(ToggleButton) findViewById(R.id.switchBasemap);
btn_edit=(Button) findViewById(R.id.editbutton);
btn_remove=(Button) findViewById(R.id.removebutton);
btn_cancel=(Button) findViewById(R.id.cancelbutton);
btn_clear=(Button) findViewById(R.id.clearbutton);
btn_undo=(Button) findViewById(R.id.undobutton);
btn_save=(Button) findViewById(R.id.savebutton);
tabhost=(TabHost) findViewById(R.id.tabHost);
tabhost.setup();
TabHost.TabSpec downloadSpec=tabhost.newTabSpec("download");
downloadSpec.setIndicator("下载");
downloadSpec.setContent(R.id.offlinedownLoadLayout);
tabhost.addTab(downloadSpec);
TabHost.TabSpec editSpec=tabhost.newTabSpec("edit");
editSpec.setIndicator("编辑");
editSpec.setContent(R.id.offlinereditlayout);
tabhost.addTab(editSpec);
TabHost.TabSpec sycnSpec=tabhost.newTabSpec("sycn");
sycnSpec.setIndicator("编辑");
sycnSpec.setContent(R.id.offlinersynclayout);
tabhost.addTab(sycnSpec);
}
/**
* 刷新
*/
public void loadLayers(View v) {
}
/**
* 现在GBD
*/
public void downloadGdb(View v) {
}
/**
* 切换底图
*/
public void switchLocalBasemap(View v) {
}
/**
* 编辑
*/
public void editButton(View v) {
}
/**
* 删除点
*/
public void removeButton(View v) {
}
/**
* 取消
*/
public void cancelButton(View v) {
}
/**
* 清除
*/
public void clearButton(View v) {
}
/**
* 撤销
*/
public void undoButton(View v) {
}
/**
* 保存
*/
public void saveButton(View v) {
}
/**
* 同步Gdb
*/
public void syncGdb(View v) {
}
}
5 接下来可以编写有关设置菜单的代码了,按照例子敲一遍xml中的offlinerpreferences.xml和string文件中offlinerstrings.xml的代码,新建一个Activity(PreferencesActivity)继承PreferenceActivity,在onCreate方法中加入addPreferencesFromResource(R.xml.offlinerpreferences); 这样PreferenceActivity就能显示程序的配置信息,会把配置信息保存到SharedPreferences中。
打开OfflineEditorActivity,覆写其中的onCreateOptionsMenu(Menu menu)和onOptionsItemSelected(MenuItem item)方法,代码如下:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.offlinepreferences, menu);
returnsuper.onCreateOptionsMenu(menu);
}
@Override
publicboolean onOptionsItemSelected(MenuItem item) {
Intent it=new Intent(this, PreferencesActivity.class);
startActivity(it);
returnsuper.onOptionsItemSelected(item);
}
6 新建一个类GDBUtil,这个类是个工具类,主要包含一些静态的方法。类中有属性为程序的配置信息,在类中新增一个静态方法:loadPreferences(OfflineEditorActivity activity)主要用来初始配置信息。主要代码如下:
public class GDBUtil {
static final String DEFAULT_FEATURE_SERVICE_URL = "http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/Wildfire/FeatureServer";
static final String DEFAULT_GDB_PATH = "/ArcGIS/samples/OfflineEditor/DamageAssess.geodatabase";
static final String DEFAULT_BASEMAP_FILENAME = "/ArcGIS/samples/OfflineEditor/SanFrancisco.tpk";
static final String DEFAULT_LAYERIDS = "0,1,2";
static final String DEFAULT_RETURN_ATTACHMENTS = "false";
static final String DEFAULT_SYNC_MODEL = "perLayer";
protected static final String TAG = "GDBUtil";
//feature服务的地址
private static String fsUrl = DEFAULT_FEATURE_SERVICE_URL;
//gdb路径
private static String gdbFileName = Environment
.getExternalStorageDirectory().getPath() + DEFAULT_GDB_PATH;
//tpk的文件路径
private static String basemapFileName = Environment
.getExternalStorageDirectory().getPath() + DEFAULT_BASEMAP_FILENAME;
//加载图层id
private static int[] layerIds = { 0 };
//是否返回Arrachments
private static boolean returnAttachments = false;
//同步模式
private static SyncModel syncModel = SyncModel.REPLICA;
public static String getBasemapFileName() {
return basemapFileName;
}
public static void setBasemapFileName(String basemapFileName) {
GDBUtil.basemapFileName =Environment.getExternalStorageDirectory().getPath()+ basemapFileName;
}
public static String getFsUrl() {
return fsUrl;
}
public static void setFsUrl(String fsUrl) {
GDBUtil.fsUrl = fsUrl;
}
public static String getGdbFileName() {
return gdbFileName;
}
public static void setGdbFileName(String gdbFileName) {
GDBUtil.gdbFileName =Environment.getExternalStorageDirectory().getPath() + gdbFileName;
}
public static int[] getLayerIds() {
return layerIds;
}
public static void setLayerIds(String layerids) {
String[] strArray = layerids.split(",");
int[] intArray = new int[strArray.length];
for (int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
GDBUtil.layerIds = intArray;
}
public static boolean isReturnAttachments() {
return returnAttachments;
}
public static void setReturnAttachments(String isreturnAttachments) {
GDBUtil.returnAttachments= isreturnAttachments.equalsIgnoreCase("true")?true:false;
}
public static SyncModel getSyncModel() {
return syncModel;
}
public static void setSyncModel(String syncModel) {
GDBUtil.syncModel = syncModel.equalsIgnoreCase("perLayer")?SyncModel.LAYER:SyncModel.REPLICA;
}
//初始化设置参数
public static void loadPreferences(OfflineEditorActivity activity){
SharedPreferences setting=PreferenceManager.getDefaultSharedPreferences(activity);
setFsUrl(setting.getString("fsurl", DEFAULT_FEATURE_SERVICE_URL));
setGdbFileName(setting.getString("gdbfilename", DEFAULT_GDB_PATH));
setLayerIds(setting.getString("layerIds", DEFAULT_LAYERIDS));
setReturnAttachments(setting.getString("returnAttachments", DEFAULT_RETURN_ATTACHMENTS));
setSyncModel(setting.getString("syncModel", DEFAULT_SYNC_MODEL));
setBasemapFileName(setting.getString("tpkfilename", DEFAULT_BASEMAP_FILENAME));
}
}
到这里为止程序的界面和配置就基本完成了,接下来就要完成各个按钮的事件了。