抽取API中提供的例子(PopupInWebMapForViewing)一部分代码,并稍加改变,是使得只显示ArcGISDynamicMapServiceLayer类型的popop,是代码能更容易理解。由于API例子中提供的webmap其实不包含ArcGISDynamicMapServiceLayer的地图,所以自己在http://www.arcgis.com找了一个ArcGISDynamicMapServiceLayer类型地图在配置弹出窗口,下面代码所使用的webmap就是使用这个共享的地图。使用下面代码能够实现,当触摸地图,根据触摸的点查找ArcGISDynamicMapServiceLayer图层中Graphic,并将其中包含的popup信息以全屏的形式显示出来。
1.定义变量:
MapView mMapView ;
ProgressDialog dio;
int count;//代表还需要查询多少个图层
PopupContainer container;//储存Popup
myPopupViewDialog popDio;//自定定义PopupContainerView显示方式
其中PopupContainer是负责储存Popup的,而myPopupViewDialog是一个自己定义的Dialog,能够把PopupContainer中的PopupContainerView按照自己定义形式显示出来。
自定义myPopupViewDialog代码如下:
//点定义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);
}
}
2 设置MapView的触摸事件,遍历MapView中所有图层,找出ArcGISDynamicMapServiceLayer的图层,之后再遍历ArcGISDynamicMapServiceLayer中所有子图层,找出包含popupInfo信息的图层,按照当前的触摸点进行查询,代码如下:
public void onSingleTap(float x, float y) {
container=new PopupContainer(mMapView);
count=0;
popDio=null;
dio.show();//显示ProgressDialog
Point p=mMapView.toMapPoint(x, y);
//得到所有图层
Layer[] layers= mMapView.getLayers();
//便利所有图层,找到ArcGISDynamicMapServiceLayer类型的图层
for(Layer layer: layers){
ArcGISDynamicMapServiceLayer ads;
if(!(layer instanceof ArcGISDynamicMapServiceLayer)){
continue;
}else{
//得到ArcGISDynamicMapServiceLayer类型的图层
ads=(ArcGISDynamicMapServiceLayer) layer;
}
//获取ArcGISDynamicMapServiceLayer下面的所有图层(包括子图层)
ArcGISLayerInfo[] infos= ads.getAllLayers();
//便利infos
for(ArcGISLayerInfo layerinfo:infos){
//取得info中的popup信息
PopupInfo popupInfo=ads.getPopupInfo(layerinfo.getId());
//popupInfo为空,就跳过
if(popupInfo==null){
continue;
}
//由于会出现子图层“显示”,但父图层“不显示的”的情况,所以需要得到父图层
ArcGISLayerInfo info=layerinfo;//保存父图层,如果没有父图层就会为空
while(info!=null && info.isVisible()){
info=info.getParentLayer();
}
// 如果得到父图层,并且父图层为不可见,就跳过
if(info!=null && !info.isVisible()){
continue;
}
//得到图层的最大和最小显示比例
double maxScale = (layerinfo.getMaxScale() != 0) ? layerinfo.getMaxScale():popupInfo.getMaxScale();
double minScale = (layerinfo.getMinScale() != 0) ? layerinfo.getMinScale():popupInfo.getMinScale();
// 如果当前 地图的比例在图层的显示范围内,就执行查询操作
if ((maxScale == 0 || mMapView.getScale() > maxScale) && (minScale == 0 || mMapView.getScale() < minScale)) {
count++;
new MyQuery(p,ads.getSpatialReference()).execute(ads.getUrl() + "/" + layerinfo.getId());
}
}
}
}
3 执行查询,并从查询结果得到包含有popup的Graphic,得到他们的popup加入到PopupContainer,最后将myPopupViewDialog显示出来,代码如下:
class MyQuery extends AsyncTask<String , Void,FeatureSet>{
Point point ;
SpatialReference sr;
public MyQuery(Point point,SpatialReference sr) {
this.point = point;
this.sr=sr;
}
@Override
protected FeatureSet doInBackground(String... params) {
for (String url : params) {
//设置查询条件
Query query = new Query();
query.setInSpatialReference(sr);
query.setOutSpatialReference(sr);
query.setGeometry(point);
query.setMaxFeatures(10);
query.setOutFields(new String[] { "*" });
QueryTask queryTask = new QueryTask(url);
try {
FeatureSet results = queryTask.execute(query);
return results;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(FeatureSet result) {
//如果当前查询的图层为最后一个要查询的图层面,dimss掉ProgressDialog
count--;
if(count==0){
dio.dismiss();
}
Graphic[] graphics= result.getGraphics();
if(graphics==null||graphics.length==0){
return;
}
for(Graphic graphic : graphics){
//获取Popup
Popup popup=new Popup(mMapView, graphic);
//加入到PopupContainer中
container.addPopup(popup);
}
//如果当前查询的图层为最后一个要查询的图层面,将PopupContainer以自定义的形式显示出来
if(count==0){
popDio=new myPopupViewDialog(DynamicLayerPopupActivity.this);
popDio.show();
}
}
}
4 结果如下:
下载代码