关闭

Android 百度地图开发之地图初始化

530人阅读 评论(0) 收藏 举报
分类:

BaiduMapSDK_v2_4_1.so

liblocSDK3.so
locSDK_3.1.jar
baidumapapi_v2_4_1.jar

首先去百度官网下载百度SDK for Android 地址点击打开链接
申请密钥  地址点击打开链接  申请密钥以后将密钥放在AndroidManifest.xml 中
 <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="qsTe4GYfoy0lBNRMWRsLQmZ6" />

1、地图控件概述

1、MapView 地图主控件
2、MapController   一个工具类 用于控制地图的缩放(ZoomIn、ZoomOut) 和平移(pan)
3、MKMapViewListener 用于处理地图事件回调


2、初始化地图


MyApplication 中初始化 百度地图sdk ;  以下借鉴百度地图sdk demo案例


package com.pad.normal.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MyApplication extends Application {
	
    public boolean m_bKeyRight = true;
    public BMapManager mBMapManager = null;
	
	public static MyApplication mInstance;	
	public static MyApplication getInstance() {
		return mInstance;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();		
		mInstance = this;
		initEngineManager(this);   
	}
		
	public void initEngineManager(Context context) {
        if (mBMapManager == null) {
            mBMapManager = new BMapManager(context);
        }

        if (!mBMapManager.init(new MyGeneralListener())) {
            Toast.makeText(MyApplication.getInstance().getApplicationContext(), 
                    "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
        }
	}	
		
	
	/**
	 * 常用事件监听,用来处理通常的网络错误,授权验证错误等
	 */
    public static class MyGeneralListener implements MKGeneralListener {
        
        @Override
        public void onGetNetworkState(int iError) {
            if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), "您的网络出错啦!",
                    Toast.LENGTH_LONG).show();
            } else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), "输入正确的检索条件!",
                        Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onGetPermissionState(int iError) {
        	//非零值表示key验证未通过
            if (iError != 0) {
                //授权Key错误:
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), 
                        "请在 DemoApplication.java文件输入正确的授权Key,并检查您的网络连接是否正常!error: "+iError, Toast.LENGTH_LONG).show();
                MyApplication.getInstance().m_bKeyRight = false;
            } else {
            	MyApplication.getInstance().m_bKeyRight = true;
            	Toast.makeText(MyApplication.getInstance().getApplicationContext(), 
                        "key认证成功", Toast.LENGTH_LONG).show();
            }
        }
    }   
	
}

3、构造地图

package com.pad.normal.activity.fmIm;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;


public class ImPoi extends Activity {

	private MapView mMapView = null;
	private MapController mMapController = null;
	private MKMapViewListener mMapListener = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.im_poi);

		Log.i("ImPoi - onCreate=================");
		
		MyApplication application = (MyApplication)getApplication();
        if (application.mBMapManager == null) {
        	application.mBMapManager = new BMapManager(getApplicationContext());
        	application.mBMapManager.init(new MyApplication.MyGeneralListener());
        }

		mMapView = (MapView) findViewById(R.id.bmapView);

		mMapController = mMapView.getController();
		mMapController.enableClick(true);
		mMapController.setZoom(18);
		mMapView.setBuiltInZoomControls(false);

		mMapListener = new MKMapViewListener() {
			@Override
			public void onMapMoveFinish() {
				
			}

			@Override
			public void onClickMapPoi(MapPoi mapPoiInfo) {
				/**
				 * 在此处理底图poi点击事件 显示底图poi名称并移动至该点 设置过:
				 * mMapController.enableClick(true); 时,此回调才能被触发
				 */
			}

			@SuppressLint("SimpleDateFormat") @Override
			public void onGetCurrentMap(Bitmap bitmap) {
				
			}

			@Override
			public void onMapAnimationFinish() {
				System.out.println("onMapAnimationFinish =================");				
			}

			@Override
			public void onMapLoadFinish() {
				System.out.println("onMapLoadFinish =================");
			}
		};
		mMapView.regMapViewListener(application.mBMapManager,mMapListener);
	}


	@Override
	protected void onResume() {
		mMapView.onResume();
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		mMapView.onPause();
		super.onPause();
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		
		if (mMapView != null) {
		   mMapView.destroyDrawingCache();
		   mMapView.destroy();
		}
		
	}
}


4、布局文件

<?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" >

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

</LinearLayout>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6392次
    • 积分:159
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章存档
    最新评论