ArcGIS API for Android 案例教程 7

    技术2026-09-14  15

    导航与触屏操作

    现在让我们看一下对MapView进行操作,比如首先第一个问题是地图导航。现在,让我们先新建一个ArcGIS Android工程,添加一个MapView,并加入一个地图服务(和Hello World差不多)。

    运行这个程序后,不出意外你就能马上看到这个地图服务。这时,如果我想拖动一下地图,用鼠标拖动一下就可以;如果我想放大,在模拟器上双击也就可以了,其实如果是在真机上,用两个手指在屏幕上做一个放大的手势也可以;但是在模拟器上想要模拟缩小就比较麻烦了。所以,这里我想加上两个按钮,分别是“放大”、“缩小”:

    "@+id/buttonZoomIn" android:text="放大"

    android:layout_width="wrap_content" android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"/>

    "@+id/buttonZoomOut" android:text="縮小"

    android:layout_width="wrap_content" android:layout_height="wrap_content"

    android:layout_alignParentRight="true"/>

    public class Navigation extends Activity {

    private MapView map = null;

    private Button buttonZoomIn = null;

    private Button buttonZoomOut = null;

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    this.map = (MapView) findViewById(R.id.map);

    this.buttonZoomIn = (Button) findViewById(R.id.buttonZoomIn);

    this.buttonZoomOut = (Button) findViewById(R.id.buttonZoomOut);

    this.buttonZoomIn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    Navigation.this.map.zoomin();

    }

    });

    this.buttonZoomOut.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    Navigation.this.map.zoomout();

    }

    });

    }

    }

    没错,调用一下MapView的zoomin和zoomout方法就可以了,这样在运行这个程序就会是图 20这样,通过两个按钮和在地图上的拖动就可以任意缩放地图到你期望的位置了。

    图 20 放大缩小地图

    然而,我们知道在触摸屏上还有其它的操作,比如“长按”。如果我现在的程序希望用户可以通过一个“长按”的操作来获取地图上一个点的坐标,然后再进行后续的标注等其它操作,应该怎么做呢?

    在ArcGIS Android API中提供了一种事件监听类型为OnLongPressListener,MapView通过这个事件监听就可以对用户的长按事件做出反应,现在我们就给这个MapView加上一个长按监听,并在长按时在一个文本框中显示触摸处的地图坐标:

    "@+id/label"

    android:layout_width="wrap_content" android:layout_height="wrap_content"

    android:textColor="#ffffff" android:layout_centerHorizontal="true"

    android:layout_alignParentBottom="true" />

    this.map.setOnLongPressListener(new OnLongPressListener() {

    public void onLongPress(float x, float y) {

    Point pt = Navigation.this.map.toMapPoint(x, y);

    Navigation.this.label.setText("X:"+pt.getX() + " Y:" + pt.getY());

    }

    });

    运行一下程序,然后用鼠标在模拟器上模拟一个长按的操作,你可以看到底部的文本框中显示了当前的坐标值,如图 21:

    图 21 长按屏幕后显示坐标值

    MapView总共支持的事件类型如下,通过这些监听我们可以在用户进行导航和触屏操作的时候进行自己需要的处理:

    事件监听

    触发条件

    OnLongPressListener

    在MapView上长按时

    OnMapExtentChangedListener

    MapView的地图范围改变时

    OnPanListener

    在MapView中拖动地图时

    OnPinchListener

    在MapView上两指缩放操作时

    OnSingleTapListener

    在MapView上单击时

    OnStatusChangedListener

    在MapView的状态改变,比如被创建或初始化时

    OnZoomListener

    在MapView被缩放时

    MapOnTouchListener

    在MapView上按住并移动(类似绘图操作)时

    最新回复(0)