arcgis for android 学习 - (2) hello Wrold. 显示一个地图
先让我们来做个示例。如果不让我看到点成就的页面,我怕觉得自己没掌握到任何东西。
下面是我个人的一些理解:
1.MapView 是android中的以个viewGroup对象,它继承具有viewGroup的属性。它是地图视图的主面板。或者说是一个地图视图的入口。
2. 我们可以往MapView 下(内)添加多个图层(layer)。
MapView map;
map.addLayer(...)
3.我们可以构建一些地图服务图层对象。该服务需要一个URL的服务地址。
new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/BeiJing_Community_BaseMap_ENG/MapServer")
这里构建以个ArcGISTiledMapServiceLayer对象。
4.添加图层。并将该“服务图层”附件到mapView以下。
ArcGISTiledMapServiceLayeragmServiceLayer;
map.addLayer(agmServiceLayer)
4. mapView可以指定一个坐标位置。该坐标位置确定了“要显示地图的范围区域”。
Envelope initextext = new Envelope(12899459.4956466,4815363.65520802,13004178.2243971,4882704.67712717);//这里有4个坐标点,看似是一个矩形的4个顶点。
map.setExtent(initextext);//设定。。。好吧,让我们跑起来代码。
-----------
贴完整代码如下:
布局:
<?xmlversion="1.0"encoding="utf-8"?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
android:orientation ="vertical" >
< LinearLayout
android:layout_width ="fill_parent"
android:layout_height ="wrap_content" >
< Button
android:id ="@+id/btnToBeijing"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="到北京" />
< Button
android:id ="@+id/btnToShenyang"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="到沈阳" />
</ LinearLayout >
<!-- MapViewlayoutandinitialextent -->
< com.esri.android.map.MapView
android:id ="@+id/map"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent" >
</ com.esri.android.map.MapView >
</ LinearLayout >
代码:
/*Copyright2012ESRI
**AllrightsreservedunderthecopyrightlawsoftheUnitedStates
*andapplicableinternationallaws,treaties,andconventions.
*
*Youmayfreelyredistributeandusethissamplecode,withor
*withoutmodification,providedyouincludetheoriginalcopyright
*noticeanduserestrictions.
*
*Seethe�Samplecodeusagerestrictions�documentforfurtherinformation.
*
*/
packagecom.esri.arcgis.android.samples.helloworld;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importcom.esri.android.map.MapView;
importcom.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
importcom.esri.android.map.ags.ArcGISTiledMapServiceLayer;
importcom.esri.core.geometry.Envelope;
importcom.esri.core.geometry.Geometry;
public classHelloWorld extendsActivity{
MapViewmap= null;
ArcGISTiledMapServiceLayertileLayer;
Buttonm_btnToShenyang;
Buttonm_btnToBeijing;
/** Calledwhentheactivityisfirstcreated. */
public voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_btnToShenyang=(Button)findViewById(R.id.btnToShenyang);
m_btnToShenyang.setOnClickListener( newOnClickListener(){
@Override
public voidonClick(Viewarg0){
// AdddynamiclayertoMapView
map.addLayer( newcom.esri.android.map.ags.ArcGISTiledMapServiceLayer(""+
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/ShenYang_Community_BaseMap_ENG/MapServer"));
Envelopeinitextext= newEnvelope(13700260.2294766,5108777.85728174,13769970.7992726,5150359.60066882);
map.setExtent(initextext);
}
});
m_btnToBeijing=(Button)findViewById(R.id.btnToBeijing);
m_btnToBeijing.setOnClickListener( newOnClickListener(){
@Override
public voidonClick(Viewarg0){
// AdddynamiclayertoMapView
map.addLayer( newcom.esri.android.map.ags.ArcGISTiledMapServiceLayer(""+
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/BeiJing_Community_BaseMap_ENG/MapServer"));
Envelopeinitextext= newEnvelope(12899459.4956466,4815363.65520802,13004178.2243971,4882704.67712717);
map.setExtent(initextext);
}
});
// RetrievethemapandinitialextentfromXMLlayout
map=(MapView)findViewById(R.id.map);
// AddtiledlayertoMapView
tileLayer= newArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.addLayer(tileLayer);
Envelopeinitextext= newEnvelope(-20037507.0671618,-20037507.0671618,20037507.0671618,20037507.0671619);
map.setExtent(initextext);
}
protected voidonPause(){
super.onPause();
map.pause();
}
protected voidonResume(){
super.onResume();
map.unpause();
}
}
参考:
sydbc的博客:http://www.gisall.com/html/72/124272-6268.html
更多相关文章
- 类和 Json对象
- 浅析android通过jni控制service服务程序的简易流程
- Android(安卓)Service AIDL
- android用户界面-组件Widget-地图视图MapView
- Android中文API(144) —— JsonWriter
- Android之Handler用法总结
- Android架构分析之使用自定义硬件抽象层(HAL)模块
- android通过ksoap2对webservice的解析
- Android(安卓)View的介绍和使用