ArcGIS for android 要素添加 案例

来源:互联网 发布:证券行业研究员 知乎 编辑:程序博客网 时间:2024/05/21 21:44

要向地图添加要素,必须需要地图有 相应的接口


主要的难点在于理解FeatrueLayer下的applyEdit方法

public void applyEdits (Graphic[] adds, Graphic[] deletes, Graphic[] updates, CallbackListener<FeatureEditResult[][]> callback)

方法需要Graphic对象,而Graphic对象要从FeatrueLayer下的 createFeatureWithTemplate 得到

public Graphic createFeatureWithTemplate (FeatureTemplate template, Geometry geometry)

而这个方法中的template又是从FeatureType对象方法中获得。一层套一层,挺麻烦的。通过不断的了解终于弄出了一个添加要素的demo程序
package guet.aerospider;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.esri.android.map.GraphicsLayer;import com.esri.android.map.MapView;import com.esri.android.map.ags.ArcGISFeatureLayer;import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;import com.esri.android.map.event.OnSingleTapListener;import com.esri.android.map.event.OnStatusChangedListener;import com.esri.core.geometry.Point;import com.esri.core.map.CallbackListener;import com.esri.core.map.FeatureEditResult;import com.esri.core.map.FeatureTemplate;import com.esri.core.map.FeatureType;import com.esri.core.map.Graphic;public class TestActivity extends Activity {MapView mMapView ;ArcGISTiledMapServiceLayer tileLayer;ArcGISFeatureLayer fLayer;GraphicsLayer gLayer;Button featureAdd;Button featureUpdate;Button featureDel;Button featureSave;Point point;private static final int ADD = 0;protected static final String TAG = "mydemo";int editingmode = -1;    /** 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.mapview);featureAdd = (Button)findViewById(R.id.mainadd);featureDel = (Button)findViewById(R.id.maindelete);featureUpdate = (Button)findViewById(R.id.mainedit);featureSave = (Button)findViewById(R.id.mainsave);OnClickListenerImpl lis = new OnClickListenerImpl();featureAdd.setOnClickListener(lis);featureDel.setOnClickListener(lis);featureUpdate.setOnClickListener(lis);featureSave.setOnClickListener(lis);tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");fLayer = new ArcGISFeatureLayer("http://sampleserver5.arcgisonline.com/ArcGIS/rest/services/LocalGovernment/Recreation/FeatureServer/0", ArcGISFeatureLayer.MODE.ONDEMAND);mMapView.addLayer(tileLayer);mMapView.addLayer(fLayer);//设置mapview的状态改变监听mMapView.setOnStatusChangedListener(new OnStatusChangedListener(){private static final long serialVersionUID = -8650595291230795928L;public void onStatusChanged(Object source, STATUS status) {if(STATUS.INITIALIZED==status){gLayer = new GraphicsLayer();mMapView.addLayer(gLayer);featureUpdate.setEnabled(true);featureSave.setEnabled(true);featureDel.setEnabled(true);featureAdd.setEnabled(true);}}});//设置mapview的单击事件mMapView.setOnSingleTapListener(new OnSingleTapListener() {private static final long serialVersionUID = 1631794627814713202L;public void onSingleTap(float x, float y) {point = mMapView.toMapPoint(x, y);featureSave.setEnabled(true);}});    }//onCreate end        //按钮监听类    class OnClickListenerImpl implements OnClickListener{public void onClick(View v) {switch (v.getId()) {case R.id.mainadd:editingmode = ADD;featureUpdate.setEnabled(false);featureDel.setEnabled(false);featureSave.setEnabled(false);featureAdd.setEnabled(false);break;case R.id.mainsave:save();break;default:break;}}    }     private void save() {    //添加操作,默认添加camptingif (editingmode == ADD){featureSave.setEnabled(false);Graphic g = null;FeatureType[] types = fLayer.getTypes();for (FeatureType fType : types) {FeatureTemplate[] templates = fType.getTemplates();for (FeatureTemplate fTemp : templates) {String name = fTemp.getName();Log.i(TAG, "" + name);if ("Camping".equals(name)) {g = fLayer.createFeatureWithTemplate(fTemp, point);}}}fLayer.applyEdits(new Graphic[] { g }, null, null,new CallbackListener<FeatureEditResult[][]>() {public void onError(Throwable e) {Log.d(TAG, e.getMessage());}public void onCallback(FeatureEditResult[][] objs) {Log.i(TAG, "applyEdit returns");featureUpdate.setEnabled(true);featureDel.setEnabled(true);featureSave.setEnabled(true);featureAdd.setEnabled(true);}});editingmode = -1;}}@Override protected void onDestroy() { super.onDestroy(); }@Overrideprotected void onPause() {super.onPause();mMapView.pause(); }@Override protected void onResume() {super.onResume(); mMapView.unpause();}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    tools:context=".TestActivity"    >    <LinearLayout         android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content"><Button    android:id="@+id/mainadd"      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="添加"    android:enabled="false"/><Button    android:id="@+id/mainedit"      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="修改"    android:enabled="false"/><Button    android:id="@+id/maindelete"      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="删除"    android:enabled="false"/><Button    android:id="@+id/mainsave"      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="保存"    android:enabled="false"/></LinearLayout><com.esri.android.map.MapView xmlns:android="http://schemas.android.com/apk/res/android"      android:id="@+id/mapview"      android:layout_width="fill_parent"      android:layout_height="fill_parent">  </com.esri.android.map.MapView></LinearLayout>