arcgis for android 学习 - (4) 了解mapView的一些方法和事件

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

 

---------- 

布局:

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

代码:

  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();
    }
}

 

代码下载

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值