关于百度地图的使用
在android手机上使用百度地图来定位的资料在网上很难找到,经过两天的折腾,终于搞出来了。下面的代码已经实现完整定位,需要注意的就是,如果你的百度地图的底图未显示出来,请确保你手机能使用GPRS(以移动为例)或WIFI联网,要使用精确定位的话,也必须要打开GPS。下面我们直接上代码,代码说明,百度里面有,一看就知道:
step1 AndroidManifest.xml文件
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.studio.basf.mapdemo" android:versionCode="1"
4 android:versionName="1.0">
5 <application android:icon="@drawable/icon" android:label="@string/app_name"
6 android:name="MapManagerApplication">
7 <activity android:name=".MainActivity" android:label="@string/app_name">
8 <intent-filter>
9 <action android:name="android.intent.action.MAIN" />
10 <category android:name="android.intent.category.LAUNCHER" />
11 </intent-filter>
12 </activity>
13 </application>
14 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
15 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
16 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
17 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
18 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
19 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
20 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
21 <supports-screens android:largeScreens="true"
22 android:normalScreens="true" android:smallScreens="true"
23 android:resizeable="true" android:anyDensity="true" />
24 <uses-sdk android:minSdkVersion="4" />
25 </manifest>
step2 首先建立一个全局应用类,主要是创建一个BMapManager实例,为了方便管理
1 package com.studio.basf.mapdemo;
2
3 import android.app.Application;
4
5 import com.baidu.mapapi.BMapManager;
6 import com.baidu.mapapi.MKEvent;
7 import com.baidu.mapapi.MKGeneralListener;
8
9 public class MapManagerApplication extends Application {
10 BMapManager bMapManager = null;
11
12 static class MyGeneralListener implements MKGeneralListener {
13
14 @Override
15 public void onGetNetworkState(int iError) {
16
17 }
18
19 @Override
20 public void onGetPermissionState(int iError) {
21 if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
22
23 }
24 }
25 }
26
27 @Override
28 public void onCreate() {
29 // TODO Auto-generated method stub
30 bMapManager = new BMapManager(this);
31 bMapManager.init("你的key", new MyGeneralListener());
32 }
33 }
step3 继承MapActivity,同时实现LocationListener接口
1 package com.studio.basf.mapdemo;
2
3 import android.location.Location;
4 import android.os.Bundle;
5
6 import com.baidu.mapapi.BMapManager;
7 import com.baidu.mapapi.GeoPoint;
8 import com.baidu.mapapi.LocationListener;
9 import com.baidu.mapapi.MKLocationManager;
10 import com.baidu.mapapi.MapActivity;
11 import com.baidu.mapapi.MapController;
12 import com.baidu.mapapi.MapView;
13 import com.baidu.mapapi.MyLocationOverlay;
14
15 public class MainActivity extends MapActivity implements LocationListener {
16 private BMapManager mBMapMan = null;
17 private MapView mMapView = null;
18 private MapController mMapController = null;
19 private MKLocationManager locationManager = null;
20
21 /** Called when the activity is first created. */
22 @Override
23 public void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.main);
26
27 mBMapMan = ((MapManagerApplication) getApplication()).bMapManager;
28 if (mBMapMan == null) {
29 ((MapManagerApplication) getApplication()).bMapManager = new BMapManager(
30 getApplication());
31 ((MapManagerApplication) getApplication()).bMapManager.init(
32 "你的key",
33 new MapManagerApplication.MyGeneralListener());
34 mBMapMan = ((MapManagerApplication) getApplication()).bMapManager;
35 }
36 super.initMapActivity(mBMapMan);
37
38 locationManager = mBMapMan.getLocationManager();
39 locationManager.requestLocationUpdates(this);
40
41 mMapView = (MapView) findViewById(R.id.bmapsView);
42 mMapView.setBuiltInZoomControls(true);// 设置启用默认的缩放控件
43
44 mMapController = mMapView.getController();// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
45 mMapController.setZoom(12);// 设置地图zoom级别
46
47 MyLocationOverlay mylocTest = new MyLocationOverlay(this, mMapView);
48 mylocTest.enableMyLocation();
49 mylocTest.enableCompass();
50 mMapView.getOverlays().add(mylocTest);
51 }
52
53 @Override
54 public void onLocationChanged(Location location) {
55 // TODO Auto-generated method stub
56
57 if (location != null) {
58 mMapController.animateTo(new GeoPoint(
59 (int) (location.getLatitude() * 1E6), (int) (location
60 .getLongitude() * 1E6)));
61 }
62 }
63
64 @Override
65 protected void onDestroy() {
66 // TODO Auto-generated method stub
67 if (mBMapMan != null) {
68 mBMapMan.destroy();
69 ((MapManagerApplication) getApplication()).bMapManager = null;
70 mBMapMan = null;
71 }
72 super.onDestroy();
73 }
74
75 @Override
76 protected void onPause() {
77 // TODO Auto-generated method stub
78 if (mBMapMan != null) {
79 mBMapMan.stop();
80 }
81 super.onPause();
82 }
83
84 @Override
85 protected void onResume() {
86 // TODO Auto-generated method stub
87 if (mBMapMan != null) {
88 mBMapMan.start();
89 }
90 super.onResume();
91 }
92
93 @Override
94 protected boolean isRouteDisplayed() {
95 // TODO Auto-generated method stub
96 return false;
97 }
98 }