我写了一个示例,用于展示了几个方法和事件的使用。直接在在代码里写了注释,那么直接贴代码。

----------

布局:

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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>

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

代码:

packagecom.vir56k.arcgisDemo;


importcom.esri.android.map.MapView;
importcom.esri.android.map.event.OnLongPressListener;
importcom.esri.android.map.event.OnPanListener;
importcom.esri.android.map.event.OnPinchListener;
importcom.esri.android.map.event.OnSingleTapListener;
importcom.esri.android.map.event.OnStatusChangedListener;
importcom.esri.android.map.event.OnZoomListener;
importcom.esri.android.map.event.OnStatusChangedListener.STATUS;
importcom.esri.core.geometry.Envelope;
importcom.esri.core.geometry.Geometry;
importcom.esri.core.geometry.Point;
importcom.esri.core.geometry.Polygon;
importcom.vir56k.arcgisDemo.mapviewDetail.R;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.Toast;

public classmapviewDetailActivity extendsActivity{
Buttonm_btnZoomOut;
Buttonbtn1ZoomIn;
ButtonbtnLookCenter;
ButtonbtnToPoint;
ButtonbtnLookExtent;
ButtonbtnSetCenter;
MapViewmMapView;

Pointm_lastClickPoint; // 上次单击的点
// 图层服务的地址
finalStringURL_STREET_COLD="http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineStreetCold/MapServer";

/** Calledwhentheactivityisfirstcreated. */
@Override
public voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 处理放大按钮的事件
m_btnZoomOut=(Button)findViewById(R.id.btnZoomOut);
m_btnZoomOut.setOnClickListener( newOnClickListener(){

@Override
public voidonClick(Viewarg0){
if(mMapView.isLoaded()){
mMapView.zoomout();
}
}
});

// 处理缩小按钮的事件
btn1ZoomIn=(Button)findViewById(R.id.btn1ZoomIn);
btn1ZoomIn.setOnClickListener( newOnClickListener(){

@Override
public voidonClick(Viewarg0){
if(mMapView.isLoaded()){
mMapView.zoomin();
}
}
});

// 查看地图中心点坐标
btnLookCenter=(Button)findViewById(R.id.btnLookCenter);
btnLookCenter.setOnClickListener( newOnClickListener(){

@Override
public voidonClick(Viewarg0){
if(mMapView.isLoaded()){
/*
*PointgetCenter()ReturnsthecenteroftheMapViewasan
*ArcGISgeometryPoint.
*/
Pointp=mMapView.getCenter();
AlertMsg("地图中心点坐标(ArcGIS几何点)是:(%s,%s)",p.getX(),p.getY());
}
}
});

// 查看地图范围
btnLookExtent=(Button)findViewById(R.id.btnLookExtent);
btnLookExtent.setOnClickListener( newOnClickListener(){

@Override
public voidonClick(Viewarg0){
if(mMapView.isLoaded()){
/*
*PolygongetExtent()Returnsapolygoncomprisingoffour
*cornersofmapinmapcoordinates.
*/
Polygonp=mMapView.getExtent();
intdimension=p.getDimension();
Geometry.Typetype=p.getType();

AlertMsg("查看地图范围dimension=%s,typ=%s",dimension,type.a());
}
}
});


// btnSetCenter
btnSetCenter=(Button)findViewById(R.id.btnSetCenter);
btnSetCenter.setOnClickListener( newOnClickListener(){

@Override
public voidonClick(Viewarg0){
if(mMapView.isLoaded()){
Pointp=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( newOnClickListener(){

@Override
public voidonClick(Viewarg0){
if(mMapView.isLoaded()){
/*
*PointtoMapPoint(floatscreenx,floatscreeny)A
*conveniencemethodthatwillconvertadevice'sscreen
*coordinatestoanArcGISgeometryPointthathasthesame
*spatialcoordinatesystemastheMapView's.Point
*toMapPoint(Pointsrc)Aconveniencemethodthatwill
*convertadevice'sscreencoordinatesintoanArcGIS
*geometryPointthathasthesamespatialcoordinate
*systemastheMapView's.PointtoScreenPoint(Pointsrc)A
*conveniencemethodthatwillconvertanArcGISgeometry
*PointfromtheMapView'sspatialcoordinatesysteminto
*thedevice'sscreencoordinates.
*/

// AlertMsg("查看地图范围dimension=%s,typ=%s",
// dimension,type.a());
}
}
});

mMapView=(MapView)findViewById(R.id.map);
mMapView.addLayer( newcom.esri.android.map.ags.ArcGISTiledMapServiceLayer(
URL_STREET_COLD));

Envelopeinitextext= newEnvelope(12899459.4956466,4815363.65520802,
13004178.2243971,4882704.67712717);

mMapView.setExtent(initextext);

// 注册长按事件
mMapView.setOnLongPressListener( newOnLongPressListener(){

@Override
public voidonLongPress( floatx, floaty){
if(mMapView.isLoaded()){
AlertMsg("长按x=%s,y=%s",x,y);
}
;

}
});
// 注册单击事件
mMapView.setOnSingleTapListener( newOnSingleTapListener(){

@Override
public voidonSingleTap( floatx, floaty){
/*
*x-thexcoordinateofthesingletaplocationinscreen
*pixels.y-theycoordinateofthesingletaplocationin
*screenpixels.
*/
// x,y.都是屏幕像素坐标点
if(mMapView.isLoaded()){
AlertMsg("单击,屏幕像素坐标点:x=%s,y=%s",x,y);

PointmapPoint=mMapView.toMapPoint( newPoint(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( newOnPanListener(){

@Override
public voidprePointerUp( floatfromx, floatfromy, floattox,
floattoy){
// TODOAuto-generatedmethodstub

}

@Override
public voidprePointerMove( floatfromx, floatfromy, floattox,
floattoy){
// TODOAuto-generatedmethodstub

}

@Override
public voidpostPointerUp( floatfromx, floatfromy, floattox,
floattoy){
// TODOAuto-generatedmethodstub

}

@Override
public voidpostPointerMove( floatfromx, floatfromy, floattox,
floattoy){
// TODOAuto-generatedmethodstub

}
});

mMapView.setOnPinchListener( newOnPinchListener(){

@Override
public voidprePointersUp( floatx1, floaty1, floatx2, floaty2,
doublefactor){
// TODOAuto-generatedmethodstub

}

@Override
public voidprePointersMove( floatx1, floaty1, floatx2, floaty2,
doublefactor){
// TODOAuto-generatedmethodstub

}

@Override
public voidprePointersDown( floatx1, floaty1, floatx2, floaty2,
doublefactor){
// TODOAuto-generatedmethodstub

}

@Override
public voidpostPointersUp( floatx1, floaty1, floatx2, floaty2,
doublefactor){
// TODOAuto-generatedmethodstub

}

@Override
public voidpostPointersMove( floatx1, floaty1, floatx2,
floaty2, doublefactor){
// TODOAuto-generatedmethodstub

}

@Override
public voidpostPointersDown( floatx1, floaty1, floatx2,
floaty2, doublefactor){
// TODOAuto-generatedmethodstub

}
});

// 当mapView的状态改变时
mMapView.setOnStatusChangedListener( newOnStatusChangedListener(){

@Override
public voidonStatusChanged(Objectsource,STATUSstatus){
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( newOnZoomListener(){

@Override
public voidpreAction( floatpivotX, floatpivotY, doublefactor){
// TODOAuto-generatedmethodstub

}

@Override
public voidpostAction( floatpivotX, floatpivotY, doublefactor){
AlertMsg("缩放状态变化,factor=:%s",factor);
}
});
}

private voidAlertMsg(Stringstr,Object...arg){
Stringmsg=String.format(str,arg);
Toast.makeText( this,msg,2).show();
Log.i("AlertMsg",msg);
}

@Override
protected voidonDestroy(){
super.onDestroy();
}

@Override
protected voidonPause(){
super.onPause();
mMapView.pause();
}

@Override
protected voidonResume(){
super.onResume();
mMapView.unpause();
}
}

代码下载

更多相关文章

  1. 它们的定义android滑动菜单
  2. android MotionEvent 获取长按压时间长
  3. android中的自定义popupwindow
  4. android 获得屏幕、视图、任务栏、状态栏的高宽以及屏幕的设置
  5. [置顶] Android仿微信语音聊天
  6. Android(安卓)Fragment 生命周期图
  7. Activity跳转时 进入和出去的动画
  8. Android(安卓)LBS地图开发:地球地理GPS坐标系经纬度偏移偏差
  9. 使用镜像站同步android sdk

随机推荐

  1. android之layout配置文件解读
  2. Android中AppWidget使用方法
  3. 3步解决AS提示:Compilation is not suppor
  4. Android Porting Steps for ARM
  5. Ubuntu12.04 安装adb
  6. Android:shape属性详解(图文并茂)
  7. iOS与Android对比学之NSUserDefaults
  8. Android 4.0 gallery2 生成video thumbna
  9. android 从tomcat读取文件出错:connect fa
  10. 自动获取svn版本号并替换android版本号