ArcGIS Runtime SDK for Android 100消息弹窗显示要素信息

需求:

使用ArcGIS Runtime SDK for Android 100.x消息弹窗显示要素信息,而且是显示字段别名以及对应的属性信息

具体实现:

runtime for android版本:100.9

①原始数据

其中name字段别名是“名称”

②发布要素服务

数据需要存放在sde中,sde需要注册到server上

③代码实现


public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private Callout mCallout;

  private ServiceFeatureTable mServiceFeatureTable;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // inflate MapView from layout
    mMapView = (MapView) findViewById(R.id.mapView);
    // create an ArcGISMap with BasemapType topo
    final ArcGISMap map = new ArcGISMap(Basemap.createImagery());
    mMapView.setViewpointCenterAsync(new com.esri.arcgisruntime.geometry.Point(12962831.176,4889554.923),2500000);
    // set the ArcGISMap to the MapView
    mMapView.setMap(map);
    // get the callout that shows attributes
    mCallout = mMapView.getCallout();
    // create the service feature table
    mServiceFeatureTable = new ServiceFeatureTable(getResources().getString(R.string.sample_service_url));
    // create the feature layer using the service feature table
    final FeatureLayer featureLayer = new FeatureLayer(mServiceFeatureTable);
    // add the layer to the map
    map.getOperationalLayers().add(featureLayer);

    // set an on touch listener to listen for click events
    mMapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, mMapView) {
      @Override
      public boolean onSingleTapConfirmed(MotionEvent e) {
        // remove any existing callouts
        if (mCallout.isShowing()) {
          mCallout.dismiss();
        }
        // get the point that was clicked and convert it to a point in map coordinates
        final Point screenPoint = new Point(Math.round(e.getX()), Math.round(e.getY()));
        // create a selection tolerance
        int tolerance = 10;
        // use identifyLayerAsync to get tapped features
        final ListenableFuture<IdentifyLayerResult> identifyLayerResultListenableFuture = mMapView
            .identifyLayerAsync(featureLayer, screenPoint, tolerance, false, 1);
        identifyLayerResultListenableFuture.addDoneListener(() -> {
          try {
            IdentifyLayerResult identifyLayerResult = identifyLayerResultListenableFuture.get();
            // create a textview to display field values
            TextView calloutContent = new TextView(getApplicationContext());
            calloutContent.setTextColor(Color.BLACK);
            calloutContent.setSingleLine(false);
            calloutContent.setVerticalScrollBarEnabled(true);
            calloutContent.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
            calloutContent.setMovementMethod(new ScrollingMovementMethod());
            calloutContent.setLines(7);
            for (GeoElement element : identifyLayerResult.getElements()) {
              Feature feature = (Feature) element;
              // create a map of all available attributes as name value pairs
              Map<String, Object> attr = feature.getAttributes();
              Set<String> keys = attr.keySet();
              for (String key : keys) {
                String  aliasName=feature.getFeatureTable().getField(key).getAlias();
                Object value = attr.get(key);
                // format observed field value as date
                if (value instanceof GregorianCalendar) {
                  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
                  value = simpleDateFormat.format(((GregorianCalendar) value).getTime());
                }
                // append name value pairs to text view
                calloutContent.append(aliasName + " | " + value + "\n");
              }
              // center the mapview on selected feature
              Envelope envelope = feature.getGeometry().getExtent();
              mMapView.setViewpointGeometryAsync(envelope, 200);
              // show callout
              mCallout.setLocation(envelope.getCenter());
              mCallout.setContent(calloutContent);
              mCallout.show();
            }
          } catch (Exception e1) {
            Log.e(getResources().getString(R.string.app_name), "Select feature failed: " + e1.getMessage());
          }
        });
        return super.onSingleTapConfirmed(e);
      }
    });

  }

  @Override
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mMapView.dispose();
  }
}

其中通过String aliasName=feature.getFeatureTable().getField(key).getAlias();语句获取被点击查询要素的别名字段,通过Object value = attr.get(key);语句获取被点击查询要素对应的属性信息。

最终效果图:

参考资料:

https://developers.arcgis.com/android/java/sample-code/feature-layer-show-attributes/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值