arcgis for android API包 裡有一些例子,建議先打開hello World示例看看。我就是看了點例子,然後從網上的高人們的blog裡找資料學習的。
先讓我們來做個示例。如果不讓我看到點成就的頁面,我怕覺得自己沒掌握到任何東西。
下面是我個人的一些了解:
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以下。
ArcGISTiledMapServiceLayer agmServiceLayer;
map.addLayer(agmServiceLayer)
4. mapView可以指定一個坐标位置。該坐标位置确定了“要顯示地圖的範圍區域”。
Envelope initextext = new Envelope(12899459.4956466,4815363.65520802,13004178.2243971,4882704.67712717);//這裡有4個坐标點,看似是一個矩形的4個頂點。
map.setExtent(initextext);//設定。。。
好吧,讓我們跑起來代碼。
-----------
貼完整代碼如下:
布局:
<?xml version="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 >
<!-- MapView layout and initial extent -->
< 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 >
代碼:
package com.esri.arcgis.android.samples.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
public class HelloWorld extends Activity {
MapView map = null;
ArcGISTiledMapServiceLayer tileLayer;
Button m_btnToShenyang;
Button m_btnToBeijing;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_btnToShenyang = (Button)findViewById(R.id.btnToShenyang);
m_btnToShenyang.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// Add dynamic layer to MapView
map.addLayer( new com.esri.android.map.ags.ArcGISTiledMapServiceLayer("" +
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/ShenYang_Community_BaseMap_ENG/MapServer"));
Envelope initextext = new Envelope(13700260.2294766, 5108777.85728174, 13769970.7992726, 5150359.60066882);
map.setExtent(initextext);
}
});
m_btnToBeijing = (Button)findViewById(R.id.btnToBeijing);
m_btnToBeijing.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// Add dynamic layer to MapView
map.addLayer( new com.esri.android.map.ags.ArcGISTiledMapServiceLayer("" +
"http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/BeiJing_Community_BaseMap_ENG/MapServer"));
Envelope initextext = new Envelope(12899459.4956466,4815363.65520802,13004178.2243971,4882704.67712717);
map.setExtent(initextext);
}
});
// Retrieve the map and initial extent from XML layout
map = (MapView)findViewById(R.id.map);
// Add tiled layer to MapView
tileLayer = new ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
map.addLayer(tileLayer);
Envelope initextext = new Envelope(-20037507.0671618,-20037507.0671618, 20037507.0671618, 20037507.0671619);
map.setExtent(initextext);
}
protected void onPause() {
super.onPause();
map.pause();
}
protected void onResume() {
super.onResume();
map.unpause();
}
}