【百度地图开发之四】百度地图基本操作功能实现讲解(博客地址:http://blog.csdn.net/developer_jiangqq),转载请注明。
Author:hmjiangqq
Email:jiangqqlmj@163.com
上一篇我们学习了【百度地图开发之三】百度地图UI控制功能讲解(点击跳转),今天继续看一下百度地图一些基本的操作功能的实现;
(一)基本介绍:
百度地图的一些基本操作控制包括地图的缩放,旋转,俯视,单双击,长按等控制事件,实现前面的三个功能主要需要用到MapController类,单双击,长按事件需要用到MapView的监听事件处理接口MKMapTouchListener
(二)控制类,接口基本介绍
1:MapController:地图操作控制器,今天我们主要用到了其中的以下方法:
①:public float setZoom(float zoomLevel):设置地图的缩放级别,这个值的取值范围为[3,19];
②:public void setRotation(int rotate):设置地图的旋转角度
③:public void setOverlooking(int voidlook):设置地图的俯视角度
2:MKMapTouchListener:地图点击事件监听器接口,主要有以下方法:
①:void onMapClick(GeoPoint point):地图单击事件,回调回来回来点击的地图的坐标点位置.
②:void onMapDoubleClick(GeoPoint point):地图双击事件,回调双击的地图的坐标点位置.
③:void onMapLongClick(GeoPoint point):地图的长按事件,回调长按的地图的坐标点位置.
(三):事件监听器使用方法:
我们只要在MapView对象中注册MKMapTouchListener监听器,并且实现其中的单击,双击,长按事件方法即可.具体实现方法如下:
/**
* 设置地图点击事件监听
*/
mapTouchListener = new MKMapTouchListener(){
@Override
public void onMapClick(GeoPoint point) {
touchType = "单击";
currentPt = point;
updateMapState();
}
@Override
public void onMapDoubleClick(GeoPoint point) {
touchType = "双击";
currentPt = point;
updateMapState();
}
@Override
public void onMapLongClick(GeoPoint point) {
touchType = "长按";
currentPt = point;
updateMapState();
}
};
//给地图注册事件
mMapView.regMapTouchListner(mapTouchListener);
(四)下面我们来看下百度地图SDK中demo的例子(代码中的注释,在此基础上已经添加的很详细了)
1:MyApplication.java:进行BMapManager全局变量的设置和密钥验证,代码如下:
package com.ztt.baidumap.ui;2:布局文件:
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();
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>3:功能实现Activity代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip">
<Button
android:id="@+id/zoombutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
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:background="@drawable/button_style"
android:text="旋转" />
<EditText
android:id="@+id/rotateangle"
android:text="90"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:id="@+id/overlookbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/button_style"
android:text="俯视" />
<EditText
android:id="@+id/overlookangle"
android:text="-30"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<TextView
android:id="@+id/state"
android:text="点击、长按、双击地图以获取经纬度和地图状态"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
/>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<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_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dip"
android:background="@drawable/button_style"
android:text="截图" />
</RelativeLayout>
</LinearLayout>
package com.ztt.baidumap.ui;4:运行截图:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MKMapTouchListener;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
/**
* 演示地图缩放,旋转,视角控制
*/
public class MapControlDemo extends Activity {
/**
* MapView 是地图主控件
*/
private MapView mMapView = null;
/**
* 用MapController完成地图控制
*/
private MapController mMapController = null;
/**
* MKMapViewListener 用于处理地图事件回调
*/
MKMapViewListener mMapListener = null;
/**
* 用于截获屏坐标
*/
MKMapTouchListener mapTouchListener = null;
/**
* 当前地点击点
*/
private GeoPoint currentPt = null;
/**
* 控制按钮
*/
private Button zoomButton = null; //缩放
private Button rotateButton = null; //旋转
private Button overlookButton =null; //俯视
private Button saveScreenButton = null; //截图
/**
*
*/
private String touchType = null;
/**
* 用于显示地图状态的面板
*/
private TextView mStateBar = 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_mapcontrol);
mMapView = (MapView)findViewById(R.id.bmapView);
/**
* 获取地图控制器
*/
mMapController = mMapView.getController();
/**
* 设置地图是否响应点击事件 .
*/
mMapController.enableClick(true);
/**
* 设置地图缩放级别
*/
mMapController.setZoom(12);
mStateBar = (TextView) findViewById(R.id.state);
/**
* 初始化地图事件监听
*/
initListener();
/**
* 将地图移动至*
* 使用百度经纬度坐标,可以通过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);
}
/**
* 添加监听事件
*/
private void initListener() {
/**
* 设置地图点击事件监听
*/
mapTouchListener = new MKMapTouchListener(){
@Override
public void onMapClick(GeoPoint point) {
touchType = "单击";
currentPt = point;
updateMapState();
}
@Override
public void onMapDoubleClick(GeoPoint point) {
touchType = "双击";
currentPt = point;
updateMapState();
}
@Override
public void onMapLongClick(GeoPoint point) {
touchType = "长按";
currentPt = point;
updateMapState();
}
};
//给地图注册事件
mMapView.regMapTouchListner(mapTouchListener);
/**
* 设置地图事件监听
*/
mMapListener = new MKMapViewListener() {
@Override
public void onMapMoveFinish() {
/**
* 在此处理地图移动完成回调
* 缩放,平移等操作完成后,此回调被触发
*/
updateMapState();
}
@Override
public void onClickMapPoi(MapPoi mapPoiInfo) {
/**
* 在此处理底图poi点击事件
* 显示底图poi名称并移动至该点
* 设置过: mMapController.enableClick(true); 时,此回调才能被触发
*
*/
}
@Override
public void onGetCurrentMap(Bitmap b) {
/**
* 当调用过 mMapView.getCurrentMap()后,此回调会被触发
* 可在此保存截图至存储设备
*/
File file = new File("/mnt/sdcard/test.png");
FileOutputStream out;
try{
out = new FileOutputStream(file);
if(b.compress(Bitmap.CompressFormat.PNG, 70, 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();
}
}
@Override
public void onMapAnimationFinish() {
/**
* 地图完成带动画的操作(如: animationTo())后,此回调被触发
*/
updateMapState();
}
@Override
public void onMapLoadFinish() {
// TODO Auto-generated method stub
}
};
mMapView.regMapViewListener(MyApplication.getInstance().mBMapManager, mMapListener);
/**
* 设置按键监听
*/
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)){
//截图,在MKMapViewListener中保存图片
mMapView.getCurrentMap();
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());
//设置地图缩放级别
mMapController.setZoom(zoomLevel);
}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());
//设置地图旋转角度
mMapController.setRotation(rotateAngle);
}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());
//设置地图的俯视
mMapController.setOverlooking(overlookAngle);
}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.getLongitudeE6()*1E-6,currentPt.getLatitudeE6()*1E-6);
}
state += "\n";
state += String.format("zoom level= %.1f rotate angle= %d overlaylook angle= %d",
mMapView.getZoomLevel(),
mMapView.getMapRotation(),
mMapView.getMapOverlooking()
);
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.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);
}
}
[注明]AndroidManifest.xml的配置和之前的文章中一样.