ArcGIS Runtime SDK For Android 10.2.x版本之地圖彈框Callout

一、介紹
主要調用的是com.esri.android.map.Callout類。具體使用方式大體如下:
1、獲取到MapView的Callout組件,一個MapView只有一個Callout(可參看源碼)
2、定義Callout的樣式,通過setStyle方法
3、設置Callout的界面佈局
4、在指定點位顯示Callout組件

、效果圖如下

三、代碼示例
1、java代碼如下
/**
* 初始化地圖點擊查詢彈框
* @param feature 查詢到的要素
* @param identifyPoint Callout顯示的位置
*/
private final void initMapViewCallout(Feature feature, Point identifyPoint) {
    try {

        Callout callout = mMapView.getCallout();
        callout.setStyle(R.xml.calloutstyle);
        callout.setContent(createCallOutContent(feature));

        if (mMapView.getCallout().isShowing()) {
            mMapView.getCallout().hide();
        }

        //控制彈框位置
        if (feature.getGeometry().getType() == Geometry.Type.POINT) {
            Point point = (Point) feature.getGeometry();
            mMapView.getCallout().show(point);
        } else if (feature.getGeometry().getType() == Geometry.Type.POLYLINE) {
            Point point = GeometryEngine.getNearestCoordinate(feature.getGeometry(), identifyPoint, false).getCoordinate();
            mMapView.getCallout().show(point);
        } else if (feature.getGeometry().getType() == Geometry.Type.POLYGON) {
            if (GeometryEngine.contains(feature.getGeometry(), identifyPoint, mMapView.getSpatialReference())) {
                mMapView.getCallout().show(identifyPoint);
            } else {
                Point point = GeometryEngine.getNearestCoordinate(feature.getGeometry(), identifyPoint, false).getCoordinate();
                mMapView.getCallout().show(point);
            }
        }
    } catch (Exception e) {
        mMapView.getCallout().animatedHide();
        e.printStackTrace();
    }
}

private View createCallOutContent(Feature feature) {
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View calloutView = layoutInflater.inflate(R.layout.callout, null);
    ListView listView = (ListView) calloutView.findViewById(R.id.callout_listview);

    SimpleAdapter adapter = new SimpleAdapter(this, getCallOutData(feature), R.layout.callout_item,
            new String[]{"key", "value"},
            new int[]{R.id.key, R.id.value});
    listView.setAdapter(adapter);

    return calloutView;
}

private List<Map<String, Object>> getCallOutData(Feature feature) {
    List<Map<String, Object>> list = new ArrayList<>();

    Set set = feature.getAttributes().entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        if (entry.getKey().toString().indexOf("Shape") != -1 || entry.getKey().toString().indexOf("OBJECTID") != -1)
            continue;
        Map<String, Object> map = new HashMap<>();
        map.put("key", entry.getKey().toString());
        map.put("value", entry.getValue().toString());
        list.add(map);
    }

    return list;
}
2、Callout的style樣式如下
<?xml version="1.0" encoding="utf-8"?><resources>
    <calloutViewStyle>
        titleTextColor="#000000"      <!-- 標題顏色 -->
        titleTextSize = 10;          <!-- 標題文字大小 -->
        titleTextStyle = 0;          <!-- 字體樣式 -->
        titleTextTypeFace = 0;        <!-- 字體類型設置 -->
        backgroundColor="#ffffff"    <!-- Callout背景顏色 -->
        backgroundAlpha="230"        <!-- Callout透明度 -->
        frameColor="#E8E8E6"        <!-- 邊框顏色 -->
        flat="true"                  <!-- true表示2D圖形,false表示3D圖形 -->
        cornerCurve="0"             <!-- 邊框的角的圓潤程度 -->
        anchor="8"                  <!-- 錨點的位置-->
    </calloutViewStyle>               
</resources>

3、Callout的佈局文件如下
列表佈局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="260dp"
    android:layout_height="130dp">
    <ListView
        android:id="@+id/callout_listview"
        android:layout_width="260dp"
        android:layout_height="130dp"></ListView>
</LinearLayout>
列表裏的項目子佈局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="260dp"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/key"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="22sp" />
    <TextView android:id="@+id/value"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp" />
</LinearLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章