我寫了一個示例,用於展示了幾個方法和事件的使用。直接在在代碼里寫了注釋,那么直接貼代碼。
----------
布局:
<?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/btnZoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="縮小" />
<Button
android:id="@+id/btn1ZoomIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放大" />
<Button
android:id="@+id/btnLookExtent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看范圍" />
<Button
android:id="@+id/btnToPoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="坐標變換" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnLookCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看中點坐標" />
<Button
android:id="@+id/btnSetCenter"
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>
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/btnZoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="縮小" />
<Button
android:id="@+id/btn1ZoomIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放大" />
<Button
android:id="@+id/btnLookExtent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看范圍" />
<Button
android:id="@+id/btnToPoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="坐標變換" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnLookCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看中點坐標" />
<Button
android:id="@+id/btnSetCenter"
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.vir56k.arcgisDemo;
import com.esri.android.map.MapView;
import com.esri.android.map.event.OnLongPressListener;
import com.esri.android.map.event.OnPanListener;
import com.esri.android.map.event.OnPinchListener;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.event.OnZoomListener;
import com.esri.android.map.event.OnStatusChangedListener.STATUS;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.vir56k.arcgisDemo.mapviewDetail.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class mapviewDetailActivity extends Activity {
Button m_btnZoomOut;
Button btn1ZoomIn;
Button btnLookCenter;
Button btnToPoint;
Button btnLookExtent;
Button btnSetCenter;
MapView mMapView;
Point m_lastClickPoint; // 上次單擊的點
// 圖層服務的地址
final String URL_STREET_COLD = "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineStreetCold/MapServer";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 處理放大按鈕的事件
m_btnZoomOut = (Button) findViewById(R.id.btnZoomOut);
m_btnZoomOut.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mMapView.isLoaded()) {
mMapView.zoomout();
}
}
});
// 處理縮小按鈕的事件
btn1ZoomIn = (Button) findViewById(R.id.btn1ZoomIn);
btn1ZoomIn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mMapView.isLoaded()) {
mMapView.zoomin();
}
}
});
// 查看地圖中心點坐標
btnLookCenter = (Button) findViewById(R.id.btnLookCenter);
btnLookCenter.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mMapView.isLoaded()) {
/*
* Point getCenter() Returns the center of the MapView as an
* ArcGIS geometry Point.
*/
Point p = mMapView.getCenter();
AlertMsg("地圖中心點坐標(ArcGIS幾何點)是:(%s,%s)", p.getX(), p.getY());
}
}
});
// 查看地圖范圍
btnLookExtent = (Button) findViewById(R.id.btnLookExtent);
btnLookExtent.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mMapView.isLoaded()) {
/*
* Polygon getExtent() Returns a polygon comprising of four
* corners of map in map coordinates.
*/
Polygon p = mMapView.getExtent();
int dimension = p.getDimension();
Geometry.Type type = p.getType();
AlertMsg("查看地圖范圍dimension=%s,typ=%s", dimension, type.a());
}
}
});
// btnSetCenter
btnSetCenter = (Button) findViewById(R.id.btnSetCenter);
btnSetCenter.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mMapView.isLoaded()) {
Point p = m_lastClickPoint;
if(p != null)
{
mMapView.centerAt(p, true);
AlertMsg("設定 地圖中點為: x=%s,y=%s", p.getX(),
p.getY());
}
}
}
});
// 坐標變換
btnToPoint = (Button) findViewById(R.id.btnToPoint);
btnToPoint.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
if (mMapView.isLoaded()) {
/*
* Point toMapPoint(float screenx, float screeny) A
* convenience method that will convert a device's screen
* coordinates to an ArcGIS geometry Point that has the same
* spatial coordinate system as the MapView's. Point
* toMapPoint(Point src) A convenience method that will
* convert a device's screen coordinates into an ArcGIS
* geometry Point that has the same spatial coordinate
* system as the MapView's. Point toScreenPoint(Point src) A
* convenience method that will convert an ArcGIS geometry
* Point from the MapView's spatial coordinate system into
* the device's screen coordinates.
*/
// AlertMsg("查看地圖范圍dimension=%s,typ=%s",
// dimension,type.a());
}
}
});
mMapView = (MapView) findViewById(R.id.map);
mMapView.addLayer( new com.esri.android.map.ags.ArcGISTiledMapServiceLayer(
URL_STREET_COLD));
Envelope initextext = new Envelope(12899459.4956466, 4815363.65520802,
13004178.2243971, 4882704.67712717);
mMapView.setExtent(initextext);
// 注冊長按 事件
mMapView.setOnLongPressListener( new OnLongPressListener() {
@Override
public void onLongPress( float x, float y) {
if (mMapView.isLoaded()) {
AlertMsg("長按 x=%s,y=%s", x, y);
}
;
}
});
// 注冊單擊事件
mMapView.setOnSingleTapListener( new OnSingleTapListener() {
@Override
public void onSingleTap( float x, float y) {
/*
* x - the x coordinate of the single tap location in screen
* pixels. y - the y coordinate of the single tap location in
* screen pixels.
*/
// x,y.都是屏幕像素坐標點
if (mMapView.isLoaded()) {
AlertMsg("單擊,屏幕像素坐標點: x=%s,y=%s", x, y);
Point mapPoint = mMapView.toMapPoint( new Point(x, y));
AlertMsg("單擊,地圖坐標點: x=%s,y=%s", mapPoint.getX(),
mapPoint.getY());
m_lastClickPoint = mapPoint;
AlertMsg("設定 上次單擊的點為: x=%s,y=%s", mapPoint.getX(),
mapPoint.getY());
}
;
}
});
mMapView.setOnPanListener( new OnPanListener() {
@Override
public void prePointerUp( float fromx, float fromy, float tox,
float toy) {
// TODO Auto-generated method stub
}
@Override
public void prePointerMove( float fromx, float fromy, float tox,
float toy) {
// TODO Auto-generated method stub
}
@Override
public void postPointerUp( float fromx, float fromy, float tox,
float toy) {
// TODO Auto-generated method stub
}
@Override
public void postPointerMove( float fromx, float fromy, float tox,
float toy) {
// TODO Auto-generated method stub
}
});
mMapView.setOnPinchListener( new OnPinchListener() {
@Override
public void prePointersUp( float x1, float y1, float x2, float y2,
double factor) {
// TODO Auto-generated method stub
}
@Override
public void prePointersMove( float x1, float y1, float x2, float y2,
double factor) {
// TODO Auto-generated method stub
}
@Override
public void prePointersDown( float x1, float y1, float x2, float y2,
double factor) {
// TODO Auto-generated method stub
}
@Override
public void postPointersUp( float x1, float y1, float x2, float y2,
double factor) {
// TODO Auto-generated method stub
}
@Override
public void postPointersMove( float x1, float y1, float x2,
float y2, double factor) {
// TODO Auto-generated method stub
}
@Override
public void postPointersDown( float x1, float y1, float x2,
float y2, double factor) {
// TODO Auto-generated method stub
}
});
// 當mapView的狀態改變時
mMapView.setOnStatusChangedListener( new OnStatusChangedListener() {
@Override
public void onStatusChanged(Object source, STATUS status) {
if (status.equals(STATUS.INITIALIZATION_FAILED)) {
AlertMsg("mapView的狀態改變時:%s", "初始化失敗");
}
;
if (status.equals(STATUS.INITIALIZED)) {
AlertMsg("mapView的狀態改變時:%s", "初始化完成");
}
;
if (status.equals(STATUS.LAYER_LOADED)) {
AlertMsg("mapView的狀態改變時:%s", "圖層加載完成");
}
;
if (status.equals(STATUS.LAYER_LOADING_FAILED)) {
AlertMsg("mapView的狀態改變時:%s", "圖層加載失敗");
}
;
}
});
// 當縮放時
mMapView.setOnZoomListener( new OnZoomListener() {
@Override
public void preAction( float pivotX, float pivotY, double factor) {
// TODO Auto-generated method stub
}
@Override
public void postAction( float pivotX, float pivotY, double factor) {
AlertMsg("縮放狀態變化,factor=:%s", factor);
}
});
}
private void AlertMsg(String str, Object... arg) {
String msg = String.format(str, arg);
Toast.makeText( this, msg,2).show();
Log.i("AlertMsg", msg);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.unpause();
}
}