|
|
|
|

ArcGIS API for Android 案例教程 6

任何一个GIS应用中都少不了地图作为参考,因此,又有哪个ArcGIS Android程序能少得了MapView呢? 刚才我们一开始就在布局文件中加入了地图服务,现在我们来尝试在程序运行的时候动态操作地图服务,比如先动态添加一个地图服务。 ArcGIS Android API...

作者:牛魔王的作坊来源:http://blog.csdn.net/warrenwyf/|2011年03月11日

任何一个GIS应用中都少不了地图作为参考,因此,又有哪个ARCGIS Android程序能少得了MapView呢?

刚才我们一开始就在布局文件中加入了地图服务,现在我们来尝试在程序运行的时候动态操作地图服务,比如先动态添加一个地图服务。

ARCGIS Android API中有一个“AddLayer”例子可以用来作为参考,让我们先导入这个工程(位置:Map_View/AddLayer),运行一下看个效果先:

clip_image002

图 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>

当我点击这个按钮的时候,我希望能够在原来的切片地图服务之上再叠加一个动态服务,因此,我还需要给这个按钮添加一个点击的事件监听,一旦按钮被点击,则再加入一个地图服务。让我们看看修改过的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这样。

clip_image004 clip_image006

19 通过点击按钮来添加服务

显然,再查看一下MapView的API就知道,通过其它如removeLayer、reorderLayer等方法,我们就可以对一个地图控件中的地图服务进行灵活的操作,以实现根据需要任意显示地图服务的目的。

上一篇:ArcGIS API for Android 案例教程 5

下一篇:ArcGIS二次开发培训视频教程(48分钟)