要显示webmap中FeatureLayer中的popup window 比显示DynamicLayer中的popup window容易的多。以下代码,是在遍历查找到含有popopInfo的信息的FeatureLayer之后,并查询这些FeatureLayer在触摸处的Graphic,最后这些Graphic为参数,实例化出popup,加入到PopupContainer中待显示。具体代码如下:
public class FeaturePopupActivity extends Activity {
MapView mMapView ;
ProgressDialog dio;
int count;//代表还需要查询多少个图层
PopupContainer container;//储存Popup
myPopupViewDialog popDio;//自定定义PopupContainerView显示方式
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMapView = new MapView(this, "http://www.arcgis.com/home/item.html?id=81d2dcf906cf4df4889ec36c8dc0c1f9", "", "");
setContentView(mMapView);
dio=new ProgressDialog(this);
dio.setMessage("正在查找...");
mMapView.setOnStatusChangedListener(new OnStatusChangedListener(){
public void onStatusChanged(Object arg0, STATUS arg1) {
if(arg0==mMapView && arg1==STATUS.INITIALIZED){
mMapView.setOnSingleTapListener(new MyTouchLis());
}
}
});
}
class MyTouchLis implements OnSingleTapListener {
public void onSingleTap(float x, float y) {
container=new PopupContainer(mMapView);
dio.show();
Layer[] layers= mMapView.getLayers();
//遍历出FeatureLayer类型的图层
for(Layer layer :layers){
if(!(layer instanceof ArcGISFeatureLayer)){
continue;
}
ArcGISFeatureLayer afl=(ArcGISFeatureLayer) layer;
//判断ArcGISFeatureLayer是否包含PopopInfo
if(afl.getPopupInfo()!=null){
count++;
new QueryFeatureLayer(FeaturePopupActivity.this,x, y).execute(afl);
}
}
}
}
class QueryFeatureLayer extends AsyncTask<ArcGISFeatureLayer, Void, List<Graphic>>{
private float x;
private float y;
private Context context;
public QueryFeatureLayer(Context context,float x, float y) {
this.x = x;
this.y = y;
this.context=context;
}
@Override
protected List<Graphic> doInBackground(ArcGISFeatureLayer... params) {
ArcGISFeatureLayer afl=params[0];
int[] ids=afl.getGraphicIDs(x, y, 20);
if(ids.length==0){
return null;
}
List<Graphic> gs=new ArrayList<Graphic>();
for(int id:ids){
Graphic g= afl.getGraphic(id);
if(g==null){
continue;
}
gs.add(g);
}
return gs;
}
@Override
protected void onPostExecute(List<Graphic> result) {
count--;
if(count==0){
dio.dismiss();
}
if(result==null || result.size()==0){
return;
}
for(Graphic g:result){
container.addPopup(new Popup(mMapView, g));
}
if(count==0){
popDio=new myPopupViewDialog(context);
popDio.show();
}
}
}
//点定义PopupContainer显示显示,使其以Dialog的形式显示
class myPopupViewDialog extends Dialog{
private Context context;
public myPopupViewDialog(Context context) {
super(context);
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout layout=new LinearLayout(context);
layout.addView(container.getPopupContainerView(), LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
setContentView(layout, params);
}
}
}