参考.. 官方API demo 。。。 各种资料
以及。。
- ArcGIS for Android示例解析之高亮要素-----HighlightFeatures
- ttp://blog.csdn.net/wozaifeiyang0/article/details/7323606
- arcgis android 中 Geodatabase geodata = new Geodatabase(filepath);得到geodata为空
-
- http://zhidao.baidu.com/link?url=iH5IDhbjIKOrZE3WffmhHwJ2ZqFF8AzU8tir8qvSl_BP5AUIHiGSc3aKbqZ7MWVHS1_8Umey4tgNcm9fEm3eVX0pN5DMnhe2_Z7FoGa2ppe&qq-pf-to=pcqq.group
- ArcGIS for Android示例解析之FeatureLayer服务-----SelectFeatures
- http://bbs.hiapk.com/thread-3940420-1-1.html
过程:
通过FeatureLayer我们可以很快的查询出所选的要素,并进行渲染。下面我们梳理一下选择查询的步骤:
1、 FeatureLayer图层对象所需的参数的设置,如:自身的url设置,Options对象设置,以及选择的要素渲染的样式设置等。
2、 定义一个Query对象,并且给其设置所需的值,如:设置查询条件、是否返回几何对象、输入的空间参考、空间要素以及查询的空间关系。
3、 执行FeatureLayer对象的selectFeatures()方法。
4 、Graphic graphic = new Graphic(feature.getGeometry(),
sfs);
// add graphic to layer
mGraphicsLayer.addGraphic(graphic);
详细:先
1.GraphicsLayer mGraphicsLayer = new GraphicsLayer(); //类似于画布
final String tpkPath = "/Arcgis/hello.tpk";
public static final String GEO_FILENAME="/Arcgis/hello/data/z01.geodatabase";//数据存放地址
2. onCreate里面加 mMapView.addLayer(mGraphicsLayer);
3.class TouchListener extends MapOnTouchListener {
长按清除所有画布的要素
- public void onLongPress(MotionEvent point) {
- // Our long press will clear the screen
- mStops.clearFeatures();
- mGraphicsLayer.removeAll();
- mMapView.getCallout().hide();
- }
- public boolean onSingleTap(MotionEvent point) {
- Point mapPoint = mMapView.toMapPoint(point.getX(), point.getY());
- Log.i("zjx",""+mapPoint.getX()/1000+",,,"+(mapPoint.getY()/1000+1));
- Graphic graphic = new Graphic(mapPoint, new SimpleMarkerSymbol(Color.BLUE, 10, STYLE.DIAMOND));
- mGraphicsLayer.addGraphic(graphic);}
单击得到相对地图的位置
双击。。这里我订死了查询条件,显示要素
Geodatabase geodatabase =null;
try {
geodatabase =new Geodatabase(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<GeodatabaseFeatureTable> table = geodatabase
.getGeodatabaseTables();
Log.i("zjx","list:"+table);
GeodatabaseFeatureTable mytable=geodatabase.getGeodatabaseFeatureTableByLayerId(0);
得到数据库 以及第一个数据表
- FeatureLayer featureLayer = new FeatureLayer(mytable);
- QueryParameters qParameters = new QueryParameters();
- String whereClause = "name='z5'";
- // SpatialReference sr = SpatialReference.create(102100);
- qParameters.setGeometry(mMapView.getExtent());
- // qParameters.setOutSpatialReference(sr);
- qParameters.setReturnGeometry(true);
- qParameters.setWhere(whereClause);
- CallbackListener<FeatureResult> callback=new CallbackListener<FeatureResult>(){
- public void onError(Throwable e) {
- e.printStackTrace();
- }
-
- public void onCallback(FeatureResult featureIterator) {
- //...
- Log.i("zjx","featureIterator.featureCount():"+featureIterator.featureCount());
- Log.i("zjx","featureIterator.getDisplayFieldName()"+featureIterator.getDisplayFieldName());
- Log.i("zjx","i m callback");
- }
-
-
- };
- Log.i("zjx","sb:"+featureLayer.selectFeatures(qParameters, FeatureLayer.SelectionMode.NEW,callback));
- // featureLayer.getU
- Future<FeatureResult> resultFuture=featureLayer.selectFeatures(qParameters, FeatureLayer.SelectionMode.NEW,callback);
- Log.i("zjx","resultFuture:"+ resultFuture);
- try{
- FeatureResult results = resultFuture.get();//最关键 得到结果
- Log.i("zjx","feature.featureCount():"+results.featureCount());//得到结果的数量
- Log.i("zjx","feature.getDisplayFieldName():"+results.getDisplayFieldName());
- if (results != null) {
- Log.i("zjx","results no null");
- int size = (int) results.featureCount();
- int i = 0;
- for (Object element : results) {//得到每个要素
- Log.i("zjx","the element:"+element);
- if (element instanceof Feature) {
- Log.i("zjx","element");
- Feature feature = (Feature) element;
- Log.i("zjx","Feature feature = (Feature) element;:"+element);
- // turn feature into graphic
- Random r = new Random();
- int color = Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255));
- SimpleFillSymbol sfs = new SimpleFillSymbol(color);
- sfs.setAlpha(75);
- Graphic graphic = new Graphic(feature.getGeometry(),
- sfs);
- // add graphic to layer
- mGraphicsLayer.addGraphic(graphic);//显示要素
- }
- i++;
- }
- // update message with results
-
-
- }
设置查询条件 高亮样式
}
完整:
- package com.esri.arcgis.android.samples.offlineroutingandgeocoding;
-
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- import java.util.concurrent.Future;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.os.Environment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import com.esri.android.map.FeatureLayer;
- import com.esri.android.map.GraphicsLayer;
- import com.esri.android.map.GraphicsLayer.RenderingMode;
- import com.esri.android.map.MapOnTouchListener;
- import com.esri.android.map.MapView;
- import com.esri.android.map.TiledLayer;
- import com.esri.android.map.ags.ArcGISLocalTiledLayer;
- import com.esri.core.geodatabase.Geodatabase;
- import com.esri.core.geodatabase.GeodatabaseFeature;
- import com.esri.core.geodatabase.GeodatabaseFeatureTable;
- import com.esri.core.geometry.Geometry;
- import com.esri.core.geometry.Point;
- import com.esri.core.geometry.SpatialReference;
- import com.esri.core.map.CallbackListener;
- import com.esri.core.map.Feature;
- import com.esri.core.map.FeatureResult;
- import com.esri.core.map.FeatureSet;
- import com.esri.core.map.Graphic;
- import com.esri.core.symbol.SimpleFillSymbol;
- import com.esri.core.symbol.SimpleLineSymbol;
- import com.esri.core.symbol.SimpleMarkerSymbol;
- import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;
- import com.esri.core.tasks.geocode.Locator;
- import com.esri.core.tasks.geocode.LocatorReverseGeocodeResult;
- import com.esri.core.tasks.na.NAFeaturesAsFeature;
- import com.esri.core.tasks.na.Route;
- import com.esri.core.tasks.na.RouteDirection;
- import com.esri.core.tasks.na.RouteParameters;
- import com.esri.core.tasks.na.RouteResult;
- import com.esri.core.tasks.na.RouteTask;
- import com.esri.core.tasks.na.StopGraphic;
- import com.esri.core.tasks.query.QueryParameters;
-
- public class RoutingAndGeocoding extends Activity {
-
-
- MapView mMapView;
- final String extern = Environment.getExternalStorageDirectory().getPath();
- final String tpkPath = "/Arcgis/hello.tpk";
- public static final String GEO_FILENAME="/Arcgis/hello/data/z01.geodatabase";
- TiledLayer mTileLayer = new ArcGISLocalTiledLayer(extern + tpkPath);
- GraphicsLayer mGraphicsLayer = new GraphicsLayer();
- String filename=extern+GEO_FILENAME;
- RouteTask mRouteTask = null;
- NAFeaturesAsFeature mStops = new NAFeaturesAsFeature();
-
- Locator mLocator = null;
- View mCallout = null;
- Spinner dSpinner;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_routing_and_geocoding);
-
-
- dSpinner = (Spinner) findViewById(R.id.directionsSpinner);
- dSpinner.setEnabled(false);
-
-
- mMapView = (MapView) findViewById(R.id.map);
-
-
- mMapView.addLayer(mTileLayer);
- mMapView.addLayer(mGraphicsLayer);
-
-
- initializeRoutingAndGeocoding();
- mMapView.setOnTouchListener(new TouchListener(RoutingAndGeocoding.this, mMapView));
-
-
- }
-
- private void initializeRoutingAndGeocoding() {
-
-
- new Thread(new Runnable() {
-
- @Override
- public void run() {
-
- String locatorPath = "/Arcgis/hello/data/z01.loc";
- String networkPath = "/Arcgis/hello/data/z01.geodatabase";
- String networkName = "Streets_ND";
-
-
- try {
- mLocator = Locator.createLocalLocator(extern + locatorPath);
- mRouteTask = RouteTask.createLocalRouteTask(extern + networkPath, networkName);
- } catch (Exception e) {
- popToast("Error while initializing :" + e.getMessage(), true);
- e.printStackTrace();
- }
- }
- }).start();
- }
-
- class TouchListener extends MapOnTouchListener {
-
- private int routeHandle = -1;
-
- @Override
- public void onLongPress(MotionEvent point) {
-
- mStops.clearFeatures();
- mGraphicsLayer.removeAll();
- mMapView.getCallout().hide();
- }
-
- @Override
- public boolean onSingleTap(MotionEvent point) {
- Point mapPoint = mMapView.toMapPoint(point.getX(), point.getY());
- Log.i("zjx",""+mapPoint.getX()/1000+",,,"+(mapPoint.getY()/1000+1));
- Graphic graphic = new Graphic(mapPoint, new SimpleMarkerSymbol(Color.BLUE, 10, STYLE.DIAMOND));
- mGraphicsLayer.addGraphic(graphic);
- if (mLocator == null) {
- popToast("Locator uninitialized", true);
- return super.onSingleTap(point);
- }
-
- String stopAddress = "";
- try {
-
-
-
- SpatialReference mapRef = mMapView.getSpatialReference();
- LocatorReverseGeocodeResult result = mLocator.reverseGeocode(mapPoint, 50, mapRef, mapRef);
-
-
- StringBuilder address = new StringBuilder();
- if (result != null && result.getAddressFields() != null) {
- Map<String, String> addressFields = result.getAddressFields();
- address.append(String.format("%s\n%s, %s %s", addressFields.get("Street"), addressFields.get("City"),
- addressFields.get("State"), addressFields.get("ZIP")));
- }
-
-
-
- stopAddress = address.toString();
- showCallout(stopAddress, mapPoint);
-
- } catch (Exception e) {
- Log.v("Reverse Geocode", e.getMessage());
- }
-
-
- StopGraphic stop = new StopGraphic(graphic);
- stop.setName(stopAddress.toString());
- mStops.addFeature(stop);
-
- return true;
- }
-
- @Override
- public boolean onDoubleTap(MotionEvent point) {
- Log.i("zjx","double");
-
-
- Geodatabase geodatabase =null;
- try {
- geodatabase =new Geodatabase(filename);
- } catch (FileNotFoundException e) {
-
- e.printStackTrace();
- }
- List<GeodatabaseFeatureTable> table = geodatabase
- .getGeodatabaseTables();
- Log.i("zjx","list:"+table);
- GeodatabaseFeatureTable mytable=geodatabase.getGeodatabaseFeatureTableByLayerId(0);
- FeatureLayer featureLayer = new FeatureLayer(mytable);
- QueryParameters qParameters = new QueryParameters();
- String whereClause = "name='z5'";
- qParameters.setGeometry(mMapView.getExtent());
- qParameters.setReturnGeometry(true);
- qParameters.setWhere(whereClause);
- CallbackListener<FeatureResult> callback=new CallbackListener<FeatureResult>(){
- public void onError(Throwable e) {
- e.printStackTrace();
- }
-
- public void onCallback(FeatureResult featureIterator) {
-
- Log.i("zjx","featureIterator.featureCount():"+featureIterator.featureCount());
- Log.i("zjx","featureIterator.getDisplayFieldName()"+featureIterator.getDisplayFieldName());
- Log.i("zjx","i m callback");
- }
-
-
- };
- Log.i("zjx","sb:"+featureLayer.selectFeatures(qParameters, FeatureLayer.SelectionMode.NEW,callback));
- Future<FeatureResult> resultFuture=featureLayer.selectFeatures(qParameters, FeatureLayer.SelectionMode.NEW,callback);
- Log.i("zjx","resultFuture:"+ resultFuture);
- try{
- FeatureResult results = resultFuture.get();
- Log.i("zjx","feature.featureCount():"+results.featureCount());
- Log.i("zjx","feature.getDisplayFieldName():"+results.getDisplayFieldName());
- if (results != null) {
- Log.i("zjx","results no null");
- int size = (int) results.featureCount();
- int i = 0;
- for (Object element : results) {
- Log.i("zjx","the element:"+element);
- if (element instanceof Feature) {
- Log.i("zjx","element");
- Feature feature = (Feature) element;
- Log.i("zjx","Feature feature = (Feature) element;:"+element);
-
- Random r = new Random();
- int color = Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255));
- SimpleFillSymbol sfs = new SimpleFillSymbol(color);
- sfs.setAlpha(75);
- Graphic graphic = new Graphic(feature.getGeometry(),
- sfs);
-
- mGraphicsLayer.addGraphic(graphic);
- }
- i++;
- }
-
-
-
- }
-
-
- }
- catch (Exception e){
- Log.i("zjx","e:"+e);
- }
-
-
-
-
- return true;
- }
-
- public TouchListener(Context context, MapView view) {
- super(context, view);
- }
- }
-
- class DirectionsItemListener implements OnItemSelectedListener {
-
- private List<RouteDirection> mDirections;
-
- public DirectionsItemListener(List<RouteDirection> directions) {
- mDirections = directions;
- }
-
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
-
- if (mDirections != null && pos > 0 && pos <= mDirections.size())
- mMapView.setExtent(mDirections.get(pos - 1).getGeometry());
- }
-
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- }
-
- private void showCallout(String text, Point location) {
-
-
- if (mCallout == null) {
- LayoutInflater inflater = (LayoutInflater) getApplication().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mCallout = inflater.inflate(R.layout.callout, null);
- }
-
-
- ((TextView) mCallout.findViewById(R.id.calloutText)).setText(text);
- mMapView.getCallout().show(location, mCallout);
- mMapView.getCallout().setMaxWidth(700);
- }
-
- private void popToast(final String message, final boolean show) {
-
- if (!show)
- return;
-
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(RoutingAndGeocoding.this, message, Toast.LENGTH_SHORT).show();
- }
- });
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.routing_and_geocoding, menu);
- return true;
- }
-
- }
来自:http://blog.csdn.net/u011644423/article/details/45482577