您的位置:首页 > 移动开发 > Android开发

ArcGIS for Android示例解析之高亮要素-----HighlightFeatures

2012-03-06 10:08 302 查看

HighlightFeatures

要素高亮化功能,相信有其他gis开发经营的开发人员都有过相应的实现经验,对于高亮要素,简单说起来就是我们查询的或识别出来的要素进行渲染,让其突出显示而已,这个例子中涉及后面要介绍的识别的内容,我们只简单介绍相关的知识,主要介绍要素对象的渲染(也就是所谓的高亮),来看代码:

mapView.setOnLongPressListener(new OnLongPressListener() {
public void onLongPress(float x,float y) {
try {
if (tiledMapServiceLayer.isInitialized() && selectedLayerIndex >= 0) {

graphicsLayer.removeAll();
/*
*点击地图的点
*/
Point pointClicked = mapView.toMapPoint(x, y);

/*
* 识别任务所需的参数,初始化相应的值
*/
IdentifyParameters inputParameters = new IdentifyParameters();
inputParameters.setGeometry(pointClicked);
inputParameters.setLayers(new int[] { layerIndexes[selectedLayerIndex] });
Envelope env = new Envelope();
mapView.getExtent().queryEnvelope(env);
inputParameters.setSpatialReference(mapView.getSpatialReference());
inputParameters.setMapExtent(env);
inputParameters.setDPI(96);
inputParameters.setMapHeight(mapView.getHeight());
inputParameters.setMapWidth(mapView.getWidth());
inputParameters.setTolerance(10);

/*
* 这是我们自己扩展的类,在其中主要实现了IdentifyTask的请求
*/
MyIdentifyTask mIdenitfy = new MyIdentifyTask();
//执行异步操作并将参数传入异步操作中
mIdenitfy.execute(inputParameters);

} else {
Toast toast = Toast.makeText(getApplicationContext(), "Please select a layer to identify features from.",
Toast.LENGTH_SHORT);
toast.show();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});

上面的代码中,主要给地图添加了一个长按地图事件监听,在事件处理函数中主要做了初始化识别任务的参数及其执行我们扩展的MyIdentifyTask操作,MyIdentifyTask其实就是一个异步请求类,下面我们来看看,这异步请求类做了什么,代码如下:

private class MyIdentifyTask extends AsyncTask<IdentifyParameters, Void, IdentifyResult[]> {

IdentifyTask mIdentifyTask;
@Override
protected IdentifyResult[] doInBackground(IdentifyParameters... params) {
IdentifyResult[] mResult = null;
if (params != null && params.length > 0) {
IdentifyParameters mParams = params[0];//获取参数
try {
mResult = mIdentifyTask.execute(mParams);//执行识别操作
} catch (Exception e) {
e.printStackTrace();
}

}
return mResult;
}
@Override
protected void onPostExecute(IdentifyResult[] results) {
// TODO Auto-generated method stub

if (results != null && results.length > 0) {
//生成要素对象数组
highlightGraphics = new Graphic[results.length];

Toast toast = Toast.makeText(getApplicationContext(), results.length + " features identified\n",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 0);
toast.show();
for (int i = 0; i < results.length; i++) {
Geometry geom = results[i].getGeometry();
String typeName = geom.getType().name();
//在这里我们进行要素的高亮显示,也就是要素渲染工作

Random r = new Random();
int color = Color.rgb(r.nextInt(255), r.nextInt(255), r.nextInt(255));
if (typeName.equalsIgnoreCase("point")) {
SimpleMarkerSymbol sms = new SimpleMarkerSymbol(color, 20, STYLE.SQUARE);
highlightGraphics[i] = new Graphic(geom, sms);
} else if (typeName.equalsIgnoreCase("polyline")) {
SimpleLineSymbol sls = new SimpleLineSymbol(color, 5);
highlightGraphics[i] = new Graphic(geom, sls);
} else if (typeName.equalsIgnoreCase("polygon")) {
SimpleFillSymbol sfs = new SimpleFillSymbol(color);
sfs.setAlpha(75);
highlightGraphics[i] = new Graphic(geom, sfs);
}
graphicsLayer.addGraphic(highlightGraphics[i]);
clearButton.setEnabled(true);
}
} else {
Toast toast = Toast.makeText(getApplicationContext(), "No features identified.", Toast.LENGTH_SHORT);
toast.show();
}

}

@Override
protected void onPreExecute() {
mIdentifyTask = new IdentifyTask(mapURL);//初始化识别任务实例
}
}

在这里我们可以看到,这个异步类主要做了实例化识别任务对象,并且执行识别任务,返回的结果再进行渲染显示,对于Android中的异步类AsyncTask应该有所了解吧,简单介绍一下他的执行过程,当我们生成AsyncTask实例并执行execute()方法后,他的内部还是执行顺序为onPreExecute()à doInBackground()àonPostExecute()

这样我们的高亮功能示例就介绍完成了,要想实现不同的、五彩缤纷的效果那就需要我们深入了解要素的渲染类及其相关的特性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: