簡化 API:ArcGIS Android 應用程式框架

在上一篇中我們簡化了 ArcGIS Android API 在程式設計的部分,我們討論了如何利用 MapOptions 來定義 MaView 中的底圖、縮放比例和中心點來簡化編程工作流程中設置你的地圖。在這篇文章中,我們將使用討論 ArcGIS 的 Android 的應用程序框架,以進一步簡化有關與標註和反向地址定位的工作流程。

 

《使用 ArcGIS Android Application Framework》

ArcGIS Android 應用程式框架 (application framework for ArcGIS Android) 提供了簡化的類 (Class) 以協助支援部分地圖顯示和定位的工作流程。最簡單的方法是使用 Eclipse 的外掛模組:

  1. 在你的「ArcGIS for Android project」按右鍵選擇 「ArcGIS Tools > Add Application Framework to Project」。這個選項只有在「ArcGIS for Android projects」才有效。

這將會新增 arcgis-android-app-framework.jar 檔到你的專案,而你就可以開始使用這個輔助類提供給你的優勢。如果你不是使用 Eclipse 那麼你可以在你安裝 SDK 的位置「<arcgis-install-dir>/libs/directory」得到這個檔案,並且手動加入它。

 

《MapViewHelper》

MapViewHelper 為 ArcGIS Android 應用程式框架中新增幾何資料 (Geometry)、標註 (Callout)、及 MapView 的彈跳視窗 (Popup) 提供了簡化的作法。現在讓我們把焦點放在新增一個幾何資料及標註的圖形。使用 MapViewHelper 讓你可以直接的新增幾何圖形到 MapView而不需要處理 GraphicsLayer、座標參照等設定。處理新增一個點到 GraphicsLayer 並設定圖示及標註的簡化工作程式碼如下:

 

// Retrieve the map and map options from XML layout 
// Using MapOptions 
mMapView = (MapView) findViewById(R.id.map); 
// Create a MapView Helper 
mvHelper = new MapViewHelper(mMapView); 
// Create drawable icon 
icon = getResources().getDrawable(R.drawable.route_destination);
// Make sure map has loaded before adding geometries 
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() { 
  private static final long serialVersionUID = 1L; 
  public void onStatusChanged(Object source, STATUS status) { 
    // Add a graphic to represent ESRI Headquarters 
    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"); 
    } 
  } 
});

 

 

上面的程式碼中定義了一個自行定義的圖標圖形 (Graphic) 並提供了經度與緯度。當這個圖形被點擊時將會顯示一個帶有標題的標註。同時會回傳一個整數 (int) 其為這個圖形的 Unique ID,如果這個圖形無法加入 MapView 則會回傳 -1。

 

《GeocodeHelper》

GeocodeHelper 提供靜態方法為一個地址提供幾個候選點或是為提供的點產生地址資訊。這些方法簡化了為 Locator 設定參數及取得結果的流程

 

		
// initialize online locator
locator = Locator.createOnlineLocator();
// array of fields to be shown in the callout
final String[] fields = { "Address", "City", "Region", "Postal" };

// single tap on map to get the address of the location tapped
mMapView.setOnSingleTapListener(new OnSingleTapListener() {
  private static final long serialVersionUID = 1L;

  public void onSingleTap(float x, float y) {
    // remove all previous graphics
    mvHelper.removeAllGraphics();
    // show address using screen coords
    Future result = GeocodeHelper.showAddress(x, y, locator, mvHelper, null, fields,
			new CallbackListener() {
                          // handle any errors returned
                          public void onError(Throwable e) {
                            Toast.makeText(SimpleMapActivity.this, "Error",
                                 Toast.LENGTH_LONG).show();
                           }
                           // handle successful results
                          public void onCallback(LocatorReverseGeocodeResult objs) {
                            Log.d("TAG", "Reverse Geocode success!");
                           }
                         });
    }
});

 

 

上面這段程式碼中我們可以在地圖上單點一個位置,並且顯示我們設定的圖示,而我們再次點選這個圖示時將會讚一個標註將我們設定的參數顯示於其中。此外你可以參考 SimpleMap 這個範例以觀看完整的程式碼。你可以在 Android SDK 10.2 Eclipse plugin 使用「New Sample Wizard」載入這個範例。

 


 

Dan O'Neill 撰寫,於 November 15, 2013

翻譯:互動國際數位.技術服務處.鄒國信

詳細原文請參考:

http://blogs.esri.com/esri/arcgis/2013/11/15/simplified-api-part-2-arcgis-android-application-framework/