<?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="50dip"
android:orientation="horizontal" >
<Button
android:id="@+id/zoombutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="缩放" />
<EditText
android:id="@+id/zoomlevel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
<Button
android:id="@+id/rotatebutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="旋转" />
<EditText
android:id="@+id/rotateangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="90" />
<Button
android:id="@+id/overlookbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="俯视" />
<EditText
android:id="@+id/overlookangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-30" />
</LinearLayout>
<TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
android:text="点击、长按、双击地图以获取经纬度和地图状态" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<Button
android:id="@+id/savescreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dip"
android:text="截图" />
</RelativeLayout>
</LinearLayout>
/**
* 演示地图缩放,旋转,视角控制
* 介绍地图基本控制方法
*/
public class MapControlDemo extends Activity {
/**
* MapView 是地图主控件
*/
private MapView mMapView;
private BaiduMap mBaiduMap;
/**
* 当前地点击点
*/
private LatLng currentPt;
/**
* 控制按钮
*/
private Button zoomButton;
private Button rotateButton;
private Button overlookButton;
private Button saveScreenButton;
private String touchType;
/**
* 用于显示地图状态的面板
*/
private TextView mStateBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapcontrol);
mMapView = (MapView) findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
mStateBar = (TextView) findViewById(R.id.state);
initListener();
}
private void initListener() {
/**
*setOnMapTouchListener(BaiduMap.OnMapTouchListener listener):
* 设置触摸地图事件监听者
* */
mBaiduMap.setOnMapTouchListener(new OnMapTouchListener() {
//当用户触摸地图时回调函数
@Override
public void onTouch(MotionEvent event) {
}
});
/**
* setOnMapClickListener(BaiduMap.OnMapClickListener listener):
* 设置地图单击事件监听者
* */
mBaiduMap.setOnMapClickListener(new OnMapClickListener() {
//地图单击事件回调函数
public void onMapClick(LatLng point) {
touchType = "单击";
currentPt = point;
updateMapState();
}
//地图内 Poi 单击事件回调函数
//MapPoi:点击地图 Poi 点时,该兴趣点的描述信息
public boolean onMapPoiClick(MapPoi poi) {
return false;
}
});
/**
* setOnMapLongClickListener(BaiduMap.OnMapLongClickListener listener):
* 设置地图长按事件监听者
* */
mBaiduMap.setOnMapLongClickListener(new OnMapLongClickListener() {
//地图长按事件监听回调函数
public void onMapLongClick(LatLng point) {
touchType = "长按";
currentPt = point;
updateMapState();
}
});
/**
* setOnMapDoubleClickListener(BaiduMap.OnMapDoubleClickListener listener):
* 设置地图双击事件监听者
* */
mBaiduMap.setOnMapDoubleClickListener(new OnMapDoubleClickListener() {
//地图双击事件监听回调函数
public void onMapDoubleClick(LatLng point) {
touchType = "双击";
currentPt = point;
updateMapState();
}
});
/**
*setOnMapStatusChangeListener(BaiduMap.OnMapStatusChangeListener listener):
* 设置地图状态监听者
* */
mBaiduMap.setOnMapStatusChangeListener(new OnMapStatusChangeListener() {
//手势操作地图,设置地图状态等操作导致地图状态开始改变。
public void onMapStatusChangeStart(MapStatus status) {
updateMapState();
}
//地图状态改变结束
public void onMapStatusChangeFinish(MapStatus status) {
updateMapState();
}
//地图状态变化中
public void onMapStatusChange(MapStatus status) {
updateMapState();
}
});
zoomButton = (Button) findViewById(R.id.zoombutton);
rotateButton = (Button) findViewById(R.id.rotatebutton);
overlookButton = (Button) findViewById(R.id.overlookbutton);
saveScreenButton = (Button) findViewById(R.id.savescreen);
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View view) {
if (view.equals(zoomButton)) {
perfomZoom();
} else if (view.equals(rotateButton)) {
perfomRotate();
} else if (view.equals(overlookButton)) {
perfomOverlook();
} else if (view.equals(saveScreenButton)) {
// 截图,在SnapshotReadyCallback中保存图片到 sd 卡
/**
* snapshot(BaiduMap.SnapshotReadyCallback callback):
* 发起截图请求
* */
mBaiduMap.snapshot(new SnapshotReadyCallback() {
//地图截屏回调接口
public void onSnapshotReady(Bitmap snapshot) {
File file = new File("/mnt/sdcard/test.png");
FileOutputStream out;
try {
out = new FileOutputStream(file);
if (snapshot.compress(Bitmap.CompressFormat.PNG, 100, out)) {
out.flush();
out.close();
}
Toast.makeText(MapControlDemo.this,"屏幕截图成功,图片存在: " + file.toString(),
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
Toast.makeText(MapControlDemo.this, "正在截取屏幕图片...",
Toast.LENGTH_SHORT).show();
}
updateMapState();
}
};
zoomButton.setOnClickListener(onClickListener);
rotateButton.setOnClickListener(onClickListener);
overlookButton.setOnClickListener(onClickListener);
saveScreenButton.setOnClickListener(onClickListener);
}
/**
* 处理缩放 sdk 缩放级别范围: [3.0,19.0]
*/
private void perfomZoom() {
EditText t = (EditText) findViewById(R.id.zoomlevel);
try {
float zoomLevel = Float.parseFloat(t.getText().toString());//字符串转float
/**
*zoomTo(float zoom):设置地图缩放级别
* */
MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(zoomLevel);
/**
* animateMapStatus(MapStatusUpdate update):
* 以动画方式更新地图状态,动画耗时 300 ms
* */
mBaiduMap.animateMapStatus(u);
} catch (NumberFormatException e) {
Toast.makeText(this, "请输入正确的缩放级别", Toast.LENGTH_SHORT).show();
}
}
/**
* 处理旋转 旋转角范围: -180 ~ 180 , 单位:度 逆时针旋转
*/
private void perfomRotate() {
EditText t = (EditText) findViewById(R.id.rotateangle);
try {
int rotateAngle = Integer.parseInt(t.getText().toString());//字符串转int
/**
* MapStatus.Builder(MapStatus previous):构造函数,以之前的地图状态为基础。
* rotate(float rotate):设置地图旋转角度,逆时针旋转。
* build():创建地图状态对象
* */
MapStatus ms = new MapStatus.Builder(mBaiduMap.getMapStatus()).rotate(rotateAngle).build();
/**
*newMapStatus(MapStatus mapStatus):设置地图新状态
* */
MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(ms);
mBaiduMap.animateMapStatus(u);
} catch (NumberFormatException e) {
Toast.makeText(this, "请输入正确的旋转角度", Toast.LENGTH_SHORT).show();
}
}
/**
* 处理俯视 俯角范围: -45 ~ 0 , 单位: 度
*/
private void perfomOverlook() {
EditText t = (EditText) findViewById(R.id.overlookangle);
try {
int overlookAngle = Integer.parseInt(t.getText().toString());//字符串转int
/**
* MapStatus.Builder(MapStatus previous):构造函数,以之前的地图状态为基础。
* overlook(float overlook):设置地图俯仰角
* build():创建地图状态对象
* */
MapStatus ms = new MapStatus.Builder(mBaiduMap.getMapStatus()).overlook(overlookAngle).build();
MapStatusUpdate u = MapStatusUpdateFactory.newMapStatus(ms);
mBaiduMap.animateMapStatus(u);
} catch (NumberFormatException e) {
Toast.makeText(this, "请输入正确的俯角", Toast.LENGTH_SHORT).show();
}
}
/**
* 更新地图状态显示面板
*/
private void updateMapState() {
if (mStateBar == null) {
return;
}
String state = "";
if (currentPt == null) {
state = "点击、长按、双击地图以获取经纬度和地图状态";
} else {
//点击类型,经度,纬度
state = String.format(touchType + ",当前经度: %f 当前纬度:%f",
currentPt.longitude, currentPt.latitude);
}
state += "\n";
MapStatus ms = mBaiduMap.getMapStatus();
//缩放级别,旋转角度,俯视角度
state += String.format(
"zoom=%.1f rotate=%d overlook=%d",
ms.zoom, (int) ms.rotate, (int) ms.overlook);
mStateBar.setText(state);
}
@Override
protected void onPause() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
// MapView的生命周期与Activity同步,当activity恢复时需调用MapView.onResume()
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
mMapView.onDestroy();
super.onDestroy();
}
}