ArcGIS for android 要素添加 案例

本文探讨了使用ArcGIS for Android进行地图要素添加的主要方法,重点关注`applyEdits`和`createFeatureWithTemplate`两个关键函数的用法。`applyEdits`用于批量编辑,包括新增(adds)、删除(deletes)和更新(updates)要素;而`createFeatureWithTemplate`则用于根据特征模板创建带有几何信息的新特征。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


主要的难点在于理解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() {
    	//添加操作,默认添加campting
		if (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();
 }
	@Override
	protected 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>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值