问题描述:在地图上添加一个按钮,可以响应点击。但是按钮时固定的,地图拖动时,按钮不能动。如下图所示:
刚开始接触地图开发时,也曾经在这个问题上纠结过。刚刚学会了在地图上添加各种图层,无论是在地图上画大头针也好,绘制路线也好,都是使用图层来处理。
于是落入了一种思维的定式,那就是误以为在地图上添加任何标记,只能通过图层。
其实很简单,MapView也是一种控件,我们只要使用相对布局方式,就能将按钮添加到地图之上来显示。
布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.mapabc.mapapi.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="@string/maps_api_key"
android:clickable="true" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Map"
android:layout_marginRight="100dp"
/>
</RelativeLayout>
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
map.getController().animateTo(point);
}
});