編輯:關於 Android 程式設計
ArcGIS Runtime for Android 10.2 版本在易用性上做了不少改進,讓開發者在調用雲端 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 開發的門檻,讓開發者能把更多精力放在真正的業務邏輯上。
Eclipse + Android SDK 環境建置與第一個 Android 專案完整實作
想踏入 Android 開發,第一步永遠是「把環境弄起來」。本文以 Eclipse 搭配 Android SDK 與 ADT 插件為主線,從 JDK 安裝、環境變數設定、
仿微信通訊錄字母索引:Android自定義View完整實踐
一、先看效果熟悉智能手機的朋友一定不會陌生:無論是微信、小米的聯繫人,還是 QQ、美團的城市選擇頁,列表右側往往懸著一列 A~Z 的字母。手指沿著它滑動,列表便精準地跳到
Android Chronometer 計時器完整實作:倒數提醒這樣做
Chronometer 是 Android 內建的計時元件,本篇用一個「設定秒數後開始倒數、時間到就跳提醒」的小案例,把它的核心 API 全部走過一遍。 實作思路 整個
在 Windows 10 上安裝 React Native Android 開發環境
搭建 React Native 環境 首先你必須裝好 Java JDK,且版本要在 1.8 以上。下載並安裝 JDK 1.8(64 位或 32 位)後,記得配置好相關的環