參考api中所給的屬性編輯的例子,寫了一個非常簡單的屬性編輯的程式,單擊地圖中FeatureLayer的圖斑,可以實現選中圖斑,並在log中列印出該圖斑的所有屬性資訊,之後點擊修改按鈕,可以將選中圖斑中欄位為field_name的值改為“哈哈”。(更新其他屬性也可原理類似,可以參考api中提供的AttributeEditor的例子)
代碼如下:
public class AttributeEditTestActivity extends Activity {MapView mMapView ; ArcGISFeatureLayer afl; Map<String,Object> atts; String obID; ArcGISDynamicMapServiceLayer dmsl ; boolean hasSelected=false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);mMapView = (MapView) findViewById(R.id.mapV);Envelope initextent = new Envelope(-10868502.895856911, 4470034.144641369,-10837928.084542884, 4492965.25312689);mMapView.setExtent(initextent, 0);ArcGISTiledMapServiceLayer tmsl = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");mMapView.addLayer(tmsl); dmsl = new ArcGISDynamicMapServiceLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSFields/MapServer");mMapView.addLayer(dmsl);afl=new ArcGISFeatureLayer("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Petroleum/KSFields/FeatureServer/0",ArcGISFeatureLayer.MODE.SELECTION);mMapView.addLayer(afl);mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {public void onStatusChanged(Object arg0, STATUS arg1) {if(arg0==mMapView && arg1==STATUS.INITIALIZED)mMapView.setOnSingleTapListener(new mySingleTagLis());//為ArcGISFeatureLayer設定被選中的顏色SimpleFillSymbol selectSym=new SimpleFillSymbol( Color.BLUE);selectSym.setAlpha(50);selectSym.setOutline(new SimpleLineSymbol(Color.RED, 3));afl.setSelectionSymbol(selectSym);}}); } /** * 單擊地圖的監聽 */ class mySingleTagLis implements OnSingleTapListener{public void onSingleTap(float arg0, float arg1) {Point p=mMapView.toMapPoint(arg0 ,arg1);Query q=new Query();//設定Query的各個參數q.setInSpatialReference(mMapView.getSpatialReference());q.setReturnGeometry(true);q.setSpatialRelationship(SpatialRelationship.INTERSECTS);q.setGeometry(p);//執行選擇afl.selectFeatures(q, SELECTION_METHOD.NEW, new qCallBackLis());} } /** * *執行選擇ArcGISFeatureLayer的回調 */ class qCallBackLis implements CallbackListener<FeatureSet>{public void onCallback(FeatureSet fet) { if(fet.getGraphics().length>0){ hasSelected=true; Graphic g=fet.getGraphics()[0];//得到選擇的Graphic obID= g.getAttributeValue(afl.getObjectIdField()).toString();//儲存ObjectID的值 atts= g.getAttributes(); //得到包含所有屬性的Map集合,並進行遍曆之後將屬性和屬性值列印在Log的上 Set<Entry<String ,Object>> ents= atts.entrySet(); for(Entry<String ,Object> ent:ents){ Log.i("Attribute ",ent.getKey()+" : "+ent.getValue()); } }}public void onError(Throwable arg0) {Log.i("AttributeEditTestActivity", "qCallBackLis 出錯啦"+arg0.getMessage());} } public void btn_click(View v){ if(!hasSelected){ return; } Toast.makeText(getApplicationContext(), "開始提交", 0).show(); atts.put("field_name", "哈哈"); //atts.put(afl.getObjectIdField(), obID);//這裡可以不用給objectID賦值,因為atts中已經有該值了, //但在其他情況下一定要記得給objectID賦值,這樣server才知道更新那個Graphic的屬性 Graphic g=new Graphic(null, null, atts, null); afl.applyEdits(null, null, new Graphic[]{g}, new myEditLis()); } //執行更新操作的回調 class myEditLis implements CallbackListener<FeatureEditResult[][]>{public void onCallback(FeatureEditResult[][] fets) {if(fets[2]!=null && fets[2][0]!=null && fets[2][0].isSuccess()){Log.i("AttributeEditTestActivity", "edit 成功啦");runOnUiThread( new Runnable() {//需要在主線程中才能根系UIpublic void run() { dmsl.refresh();Toast.makeText(getApplicationContext(), "edit 成功啦", 0).show();}});}}public void onError(final Throwable arg0) {Log.i("AttributeEditTestActivity", "edit 出錯啦"+arg0.getMessage());runOnUiThread( new Runnable() {public void run() {Toast.makeText(getApplicationContext(), "edit 出錯啦"+arg0.getMessage(), 0).show();}});} } @Override protected void onDestroy() { super.onDestroy(); }@Overrideprotected void onPause() {super.onPause();mMapView.pause(); }@Override protected void onResume() {super.onResume(); mMapView.unpause();}
列印出來的Log:
下載全部代碼