Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

簡單易用的 ArcGIS Runtime for Android 10.2

編輯:關於 Android 程式設計

ArcGIS Runtime for Android 10.2 版本在易用性上做了不少改進,讓開發者在調用雲端 GIS 資料與實作地圖標記時都變得更輕鬆。本文就從兩個實際場景,帶你感受新版本的便利。

一、更簡單地調用雲端 GIS 資料

以前要調用 ArcGIS Online 上的地圖,你得先記住底圖的 URL 地址,並在字串資源檔中一一配置:

<string name="WORLD_STREET_MAP">https://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer</string>
<string name="WORLD_TOPO_MAP">https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer</string>
<string name="WORLD_NATGEO_MAP">https://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer</string>
<string name="OCEAN_BASEMAP">https://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer</string>

接著還得在程式碼中建立圖層、加入 MapView 並控制可見性:

// 建立初始底圖
basemapStreet = new ArcGISTiledMapServiceLayer(this.getResources()
    .getString(R.string.WORLD_STREET_MAP));
// 把底圖加入 MapView
mMapView.addLayer(basemapStreet);
// 設定可見性
basemapStreet.setVisible(true);

// 若要切換底圖
basemapTopo = new ArcGISTiledMapServiceLayer(this.getResources()
    .getString(R.string.WORLD_TOPO_MAP));
mMapView.addLayer(basemapTopo);
basemapStreet.setVisible(false);
basemapTopo.setVisible(true);

如果改用 ArcGIS Runtime for Android 10.2,這一切都變得輕鬆許多——你甚至不必再記得 ArcGIS Online 的服務地址,只要用 MapOptions 就能搞定。在 XML 中簡單配置即可:

<com.esri.android.map.MapView
    android:id="@+id/map"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    mapoptions.MapType="topo"
    mapoptions.ZoomLevel="13"
    mapoptions.center="33.666354, -117.903557"/>

然後在程式碼中只需一句:

MapView mMapView = (MapView) findViewById(R.id.map);

想切換底圖與範圍同樣輕而易舉:

MapOptions streets = new MapOptions(MapType.STREETS);
mMapView.setMapOptions(streets);

二、簡單的標記與氣泡

過去要為 graphic 實作標記與氣泡功能,需要用到 PopupInfo 與 PictureMarkerSymbol,整個過程頗為繁瑣。而有了 ArcGIS Android Application Framework,一切就簡單多了。

第一步,為專案引入 Application Framework:右鍵點擊專案,選擇「ArcGIS Tools」,再選「add Application Framework to project」,完成後即可看到引用的庫文件。

第二步,撰寫程式碼,透過 MapViewHelper 類別加入標記:

// 使用 MapOptions
mMapView = (MapView) findViewById(R.id.map);
// 建立 MapView Helper
mvHelper = new MapViewHelper(mMapView);
// 建立圖示
icon = getResources().getDrawable(R.drawable.route_destination);

// 確保地圖載入後才加入幾何物件
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
    private static final long serialVersionUID = 1L;
    public void onStatusChanged(Object source, STATUS status) {
        // 加入一個代表 ESRI 總部的標記
        int loaded = mvHelper.addMarkerGraphic(34.056695, -117.195693,
            "ESRI", "World Headquarters", null, icon, false, 0);
        if (loaded < 0) {
            Log.d("TAG", "Marker Graphic not added to MapView");
        }
    }
});

透過 MapViewHelper 的 addMarkerGraphic 方法,只需傳入座標、標題、描述與圖示,就能輕鬆完成一個帶氣泡的標記。整體而言,10.2 版本透過 MapOptions 與 Application Framework 這兩個利器,大幅降低了 GIS 開發的門檻,讓開發者能把更多精力放在真正的業務邏輯上。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved