任何一個GIS應用中都少不了地圖作為參考,因此,又有哪個ArcGIS Android程式能少得了MapView呢?
剛才我們一開始就在布局檔案中加入了地圖服務,現在我們來嘗試在程式啟動並執行時候動態操作地圖服務,比如先動態添加一個地圖服務。
ArcGIS Android API中有一個“AddLayer”例子可以用來作為參考,讓我們先匯入這個工程(位置:Map_View/AddLayer),運行一下看個效果先:
圖 18 例子AddLayer的運行效果
雖然看起來效果和上面的“Hello World”沒什麼太大的區別,只不過這裡是兩個地圖服務疊加在了一起。但是,如果開啟這個工程的原始碼,你可以發現,在布局XML中只有一個切片地圖服務,而在Activity運行時加入了另外一個地圖服務:
public class AddLayer extends Activity {
private MapView map = null;
String dynamicMapURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/
services/Specialty/ESRI_StateCityHighway_USA/MapServer";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.map = (MapView) findViewById(R.id.map);
ArcGISDynamicMapServiceLayer dynamicLayer = new ArcGISDynamicMapServiceLayer(
this, this.dynamicMapURL);
this.map.addLayer(dynamicLayer);
}
}
這段添加地圖服務的代碼實在太簡單了,想必不用多解釋什麼。如果現在我想通過一個按鈕來添加一個地圖服務,那可以把這個例子修改一下,首先,在Activity的布局中添加一個按鈕:
<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.esri.android.map.MapView android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
initExtent="-1.3296373526814876E7
3930962.41823043
-1.2807176545789773E7 4201243.7502468005">
<com.esri.android.map.ags.ArcGISTiledMapServiceLayer
android:id="@+id/tiles1"
url="http://services.arcgisonline.com/ArcGIS/rest/services
/World_Street_Map/MapServer" />
</com.esri.android.map.MapView>
<Button android:id="@+id/buttonAdd"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="添加服務" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<com.esri.android.map.MapView android:id="@+id/map"
android:layout_width="fill_parent" android:layout_height="fill_parent"
initExtent="-1.3296373526814876E7 3930962.41823043
-1.2807176545789773E7 4201243.7502468005">
<com.esri.android.map.ags.ArcGISTiledMapServiceLayer
android:id="@+id/tiles1"
url="http://services.arcgisonline.com/ArcGIS/rest/services
/World_Street_Map/MapServer" />
</com.esri.android.map.MapView>
<Button android:id="@+id/buttonAdd"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="添加服務" />
</RelativeLayout>
當我點擊這個按鈕的時候,我希望能夠在原來的切片地圖服務之上再疊加一個動態服務,因此,我還需要給這個按鈕添加一個點擊的事件監聽,一旦按鈕被點擊,則再加入一個地圖服務。讓我們看看修改過的Activity:
public class AddLayer extends Activity {
String dynamicMapURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest
/services/Specialty/ESRI_StateCityHighway_USA/MapServer";
private MapView map = null;
private Button buttonAdd = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.map = (MapView) findViewById(R.id.map);
this.buttonAdd = (Button)findViewById(R.id.buttonAdd);
this.buttonAdd.setOnClickListener(new OnClickListener(){
public void onClick(View v){
ArcGISDynamicMapServiceLayer dynamicLayer =
new ArcGISDynamicMapServiceLayer(
AddLayer.this, AddLayer.this.dynamicMapURL);
AddLayer.this.map.addLayer(dynamicLayer);
}
});
}
}
這樣,修改過的程式運行起來會出現一個“添加服務”的按鈕,而點擊這個按鈕以後,程式就會在原有的切片服務之上再疊加一個動態地圖服務,效果就像圖 19這樣。
19 通過點擊按鈕來添加服務
顯然,再查看一下MapView的API就知道,通過其它如removeLayer、reorderLayer等方法,我們就可以對一個地圖控制項中的地圖服務進行靈活的操作,以實現根據需要任意顯示地圖服務的目的。