上一篇我们学习了【百度地图开发之二】基于Fragment的地图框架的使用(点击跳转),今天继续看一下百度地图UI视图的一些控制功能.
百度地图UI控制功能主要包括一些开关手势功能和UI控件的显示与隐藏,(例如:对地图的缩放,平移,双击放大缩小,旋转,俯视,显示与隐藏缩放控件以及指南针的显示位置等功能).要实现以上的功能主要用要到下面两个类中的方法1:MapView(地图View),2:MapController:(地图控制器);下面来简要的看一下其中使用到的方法.
1:MapView:这是显示地图的视图,当该视图获得焦点的时候,可以捕捉触摸事件和点击事件手势去平移地图和缩放地图,MapView的创建我们可以通过XML布局实现,同样也可以用个构造函数去实现.本例中使用XML布局文件。同时还是用到主要下面两个方法:
①:public void setBuiltInZoomControls(boolean on) : 设置是否启用内置的缩放控件。如果on为true,MapView将自动显示这些缩放控件
②:public void setDoubleClickZooming(booean bDoubleClickZooming):设置mapView是否支持双击方法效果;
2: MapController:地图控制器:可以实现地图的手势事件,缩放,平移,选中,俯视等功能,主要用到下面几个方法:
①:public void setZoomGesturesEnabled(boolean isZoomGesturesEnable):设置缩放手势状态, true,缩放手势开启
②:public void setScrollGesturesEnabled(boolean isScrollGesturesEnable):是平移手势开关状态,true,平移手势开启
③:public voic setRotationGestureEnabled(boolean isRotationGesturesEnable):设置旋转手势开关状态,true,旋转手势开启
④:public void setOverlookingGesturedEnabled(boolean isOverlookingGesturesEnabled):设置俯视手势开关状态,true 俯视手势开启
除了这些手势方法以外,该控制器还有一些其他的方法,具体的可以到BaiduMap API文档中查看。下面根据上面的方法,我们来看下百度地图中的demo项目(代码中的注释已经写的很详细了)
1. MyApplication.java方法,进行BMapManager全局变量的设置和密钥验证,代码如下:
- package com.ztt.baidumap.ui;
- import android.app.Application;
- import android.content.Context;
- import android.util.Log;
- import android.widget.Toast;
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.MKGeneralListener;
- import com.baidu.mapapi.map.MKEvent;
- /**
- * 自定义Application,进行key识别验证 (使用单例)
- * @author Jiangqq
- * @time 2014/03/15 10:14
- */
- public class MyApplication extends Application {
- public static MyApplication instance=null;
- BMapManager mBMapManager = null;
- public boolean m_bKeyRight = true;
- public static final String strKey = "vUAGbPwLpolIqiwWisnQPeIE"; //百度地图官网申请的密钥
- public static MyApplication getInstance(){
- return instance;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- instance=this;
- //在APP应用启动的时候,进行初始化验证
- initEngineManager(this);
- }
- /**
- * 进行验证key
- * @param pContext
- */
- private void initEngineManager(Context pContext)
- {
- if (mBMapManager == null) {
- mBMapManager = new BMapManager(pContext);
- }
- if (!mBMapManager.init(strKey,new MyGeneralListener())) {
- Toast.makeText(MyApplication.getInstance(),
- "BMapManager 初始化错误!", Toast.LENGTH_LONG).show();
- }
- }
- // 常用事件监听,用来处理通常的网络错误,授权验证错误等
- static class MyGeneralListener implements MKGeneralListener {
- @Override
- public void onGetNetworkState(int iError) {
- if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
- Toast.makeText(MyApplication.getInstance(), "您的网络出错啦!",
- Toast.LENGTH_LONG).show();
- }
- else if (iError == MKEvent.ERROR_NETWORK_DATA) {
- Toast.makeText(MyApplication.getInstance(), "输入正确的检索条件!",
- Toast.LENGTH_LONG).show();
- }else {
- Log.d("zttjiangqq", "iError="+iError);
- }
- // ...
- }
- @Override
- public void onGetPermissionState(int iError) {
- //非零值表示key验证未通过
- if (iError != 0) {
- //授权Key错误:
- Toast.makeText(MyApplication.getInstance(),
- "请在 DemoApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常!error: "+iError, Toast.LENGTH_LONG).show();
- MyApplication.getInstance().m_bKeyRight = false;
- }
- else{
- MyApplication.getInstance().m_bKeyRight = true;
- Toast.makeText(MyApplication.getInstance(),
- "key认证成功", Toast.LENGTH_LONG).show();
- }
- }
- }
- }
2.布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="50dip">
- <CheckBox
- android:id="@+id/zoom"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="true"
- android:onClick="setZoomEnable"
- android:text="缩放" />
- <CheckBox
- android:id="@+id/scroll"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="true"
- android:onClick="setScrollEnable"
- android:text="平移" />
- <CheckBox
- android:id="@+id/doubleClick"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="true"
- android:onClick="setDoubleClickEnable"
- android:text="双击放大" />
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="50dip">
- <CheckBox
- android:id="@+id/rotate"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="true"
- android:onClick="setRotateEnable"
- android:text="旋转" />
- <CheckBox
- android:id="@+id/overlook"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="true"
- android:onClick="setOverlookEnable"
- android:text="俯视" />
- <CheckBox
- android:id="@+id/zoomControl"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:checked="false"
- android:onClick="setBuiltInZoomControllEnable"
- android:text="缩放控件" />
- </LinearLayout>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="50dip">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:paddingTop="10dip"
- android:text="指南针位置"
- />
- <RadioGroup
- android:id="@+id/RadioGroup"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:orientation="horizontal"
- android:text="指南针位置"
- >
- <RadioButton
- android:id="@+id/lefttop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="setCompassLocation"
- android:checked="true"
- android:text="左上角"
- />
- <RadioButton
- android:id="@+id/righttop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="setCompassLocation"
- android:text="右上角"
- />
- </RadioGroup>
- </LinearLayout>
- <com.baidu.mapapi.map.MapView android:id="@+id/bmapView"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:clickable="true"
- />
- </LinearLayout>
3.Activity具体功能实现:
- package com.ztt.baidumap.ui;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.CheckBox;
- import android.widget.RadioButton;
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.map.MapController;
- import com.baidu.mapapi.map.MapView;
- import com.baidu.platform.comapi.basestruct.GeoPoint;
- /**
- * 演示地图UI控制功能
- */
- public class UISettingDemo extends Activity {
- /**
- * MapView 是地图主控件
- */
- private MapView mMapView = null;
- /**
- * 用MapController完成地图控制
- */
- private MapController mMapController = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /**
- * 使用地图sdk前需先初始化BMapManager.
- * BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建,
- * 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁
- */
- MyApplication app = (MyApplication)this.getApplication();
- if (app.mBMapManager == null) {
- app.mBMapManager = new BMapManager(getApplicationContext());
- /**
- * 如果BMapManager没有初始化则初始化BMapManager
- */
- app.mBMapManager.init(MyApplication.strKey,new MyApplication.MyGeneralListener());
- }
- /**
- * 由于MapView在setContentView()中初始化,所以它需要在BMapManager初始化之后
- */
- setContentView(R.layout.activity_uisetting);
- mMapView = (MapView)findViewById(R.id.bmapView);
- /**
- * 获取地图控制器
- */
- mMapController = mMapView.getController();
- /**
- * 设置地图是否响应点击事件 .
- */
- mMapController.enableClick(true);
- /**
- * 设置地图缩放级别
- */
- mMapController.setZoom(12);
- /**
- * 设置地图俯角
- */
- mMapController.setOverlooking(-30);
- /**
- * 将地图移动至天安门
- * 使用百度经纬度坐标,可以通过http://api.map.baidu.com/lbsapi/getpoint/index.html查询地理坐标
- * 如果需要在百度地图上显示使用其他坐标系统的位置,请发邮件至mapapi@baidu.com申请坐标转换接口
- */
- double cLat = 39.945 ;
- double cLon = 116.404 ;
- GeoPoint p = new GeoPoint((int)(cLat * 1E6), (int)(cLon * 1E6));
- mMapController.setCenter(p);
- }
- /**
- * 是否启用缩放手势
- * @param v
- */
- public void setZoomEnable(View v){
- //缩放手势
- mMapController.setZoomGesturesEnabled(((CheckBox) v).isChecked());
- }
- /**
- * 是否启用平移手势
- * @param v
- */
- public void setScrollEnable(View v){
- //平移
- mMapController.setScrollGesturesEnabled(((CheckBox) v).isChecked());
- }
- /**
- * 是否启用双击放大
- * @param v
- */
- public void setDoubleClickEnable(View v){
- //双击
- mMapView.setDoubleClickZooming(((CheckBox) v).isChecked());
- }
- /**
- * 是否启用旋转手势
- * @param v
- */
- public void setRotateEnable(View v){
- //旋转
- mMapController.setRotationGesturesEnabled(((CheckBox) v).isChecked());
- }
- /**
- * 是否启用俯视手势
- * @param v
- */
- public void setOverlookEnable(View v){
- //俯视
- mMapController.setOverlookingGesturesEnabled(((CheckBox) v).isChecked());
- }
- /**
- * 是否显示内置绽放控件
- * @param v
- */
- public void setBuiltInZoomControllEnable(View v){
- //缩放控件
- mMapView.setBuiltInZoomControls(((CheckBox) v).isChecked());
- }
- /**
- * 设置指南针位置,指南针在3D模式下自动显现
- * @param view
- */
- public void setCompassLocation(View view){
- boolean checked = ((RadioButton) view).isChecked();
- switch(view.getId()) {
- case R.id.lefttop:
- if (checked)
- //设置指南针显示在左上角
- mMapController.setCompassMargin(100, 100);
- break;
- case R.id.righttop:
- if (checked)
- mMapController.setCompassMargin(mMapView.getWidth() - 100, 100);
- break;
- }
- }
- @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.destroy();
- super.onDestroy();
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- mMapView.onSaveInstanceState(outState);
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- mMapView.onRestoreInstanceState(savedInstanceState);
- }
- }
4:运行截图: