在Android手机上使用百度地图来定位的资料在网上很难找到,经过两天的折腾,终于搞出来了。下面的代码已经实现完整定位,需要注意的就是,如果你的 百度地图的底图未显示出来,请确保你手机能使用GPRS(以移动为例)或WIFI联网,要使用精确定位的话,也必须要打开GPS。下面我们直接上代码,代 码说明,百度里面有,一看就知道:
step1 AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.studio.basf.mapdemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MapManagerApplication"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <supports-screens android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:resizeable="true" android:anyDensity="true" /> <uses-sdk android:minSdkVersion="4" /> </manifest>
step2 首先建立一个全局应用类,主要是创建一个BMapManager实例,为了方便管理
package com.studio.basf.mapdemo; import android.app.Application; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.MKEvent; import com.baidu.mapapi.MKGeneralListener; public class MapManagerApplication extends Application { BMapManager bMapManager = null; static class MyGeneralListener implements MKGeneralListener { @Override public void onGetNetworkState(int iError) { } @Override public void onGetPermissionState(int iError) { if (iError == MKEvent.ERROR_PERMISSION_DENIED) { } } } @Override public void onCreate() { // TODO Auto-generated method stub bMapManager = new BMapManager(this); bMapManager.init("你的key", new MyGeneralListener()); } }
step3 继承MapActivity,同时实现LocationListener接口
package com.studio.basf.mapdemo; import android.location.Location; import android.os.Bundle; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.LocationListener; import com.baidu.mapapi.MKLocationManager; import com.baidu.mapapi.MapActivity; import com.baidu.mapapi.MapController; import com.baidu.mapapi.MapView; import com.baidu.mapapi.MyLocationOverlay; public class MainActivity extends MapActivity implements LocationListener { private BMapManager mBMapMan = null; private MapView mMapView = null; private MapController mMapController = null; private MKLocationManager locationManager = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBMapMan = ((MapManagerApplication) getApplication()).bMapManager; if (mBMapMan == null) { ((MapManagerApplication) getApplication()).bMapManager = new BMapManager( getApplication()); ((MapManagerApplication) getApplication()).bMapManager.init( "你的key", new MapManagerApplication.MyGeneralListener()); mBMapMan = ((MapManagerApplication) getApplication()).bMapManager; } super.initMapActivity(mBMapMan); locationManager = mBMapMan.getLocationManager(); locationManager.requestLocationUpdates(this); mMapView = (MapView) findViewById(R.id.bmapsView); mMapView.setBuiltInZoomControls(true);// 设置启用默认的缩放控件 mMapController = mMapView.getController();// 得到mMapView的控制权,可以用它控制和驱动平移和缩放 mMapController.setZoom(12);// 设置地图zoom级别 MyLocationOverlay mylocTest = new MyLocationOverlay(this, mMapView); mylocTest.enableMyLocation(); mylocTest.enableCompass(); mMapView.getOverlays().add(mylocTest); } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub if (location != null) { mMapController.animateTo(new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location .getLongitude() * 1E6))); } } @Override protected void onDestroy() { // TODO Auto-generated method stub if (mBMapMan != null) { mBMapMan.destroy(); ((MapManagerApplication) getApplication()).bMapManager = null; mBMapMan = null; } super.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub if (mBMapMan != null) { mBMapMan.stop(); } super.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub if (mBMapMan != null) { mBMapMan.start(); } super.onResume(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
因为代码在需要在真机上运行才能看到效果,所以我这里就截图,上面的代码直接复制下来就可以使用。