Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位
一、問(wèn)題描述
使用百度地圖實(shí)現(xiàn)如圖所示應(yīng)用,首先自動(dòng)定位當(dāng)前我起始位置(小圓點(diǎn)位置),并跟隨移動(dòng)不斷自動(dòng)定位我的當(dāng)前位置
百度Api不同版本使用會(huì)有些差異,本例中加入lib如下:
二、編寫(xiě)MyApplication類(lèi)
public class MyApplication extends Application { static MyApplication myApplication; BMapManager mBMapManager = null; String mStrKey = "7ZfuRcOx1G3oZ8TKuTNGm4sO"; boolean m_bKeyRight = true; // 授權(quán)Key正確,驗(yàn)證通過(guò) private List<Activity> activityList = new LinkedList<Activity>(); @Override public void onCreate() { myApplication = this; initEngineManager(this); super.onCreate(); } public void initEngineManager(Context context) { if (mBMapManager == null) { mBMapManager = new BMapManager(context); Toast.makeText(MyApplication.getInstance().getApplicationContext(), "BMapManager 初始化SUCSUC!", Toast.LENGTH_LONG).show(); } if (!mBMapManager.init(mStrKey, new MyGeneralListener())) { Toast.makeText(MyApplication.getInstance().getApplicationContext(), "BMapManager 初始化錯(cuò)誤!", Toast.LENGTH_LONG).show(); } } public static MyApplication getInstance() { if (myApplication == null) { myApplication = new MyApplication(); } return myApplication; } public void addActivity(Activity activity) { activityList.add(activity); } public void exit() { for (Activity activity : activityList) { activity.finish(); } System.exit(0); } // 常用事件監(jiān)聽(tīng),用來(lái)處理通常的網(wǎng)絡(luò)錯(cuò)誤,授權(quán)驗(yàn)證錯(cuò)誤等 static class MyGeneralListener implements MKGeneralListener { public void onGetNetworkState(int iError) { if (iError == MKEvent.ERROR_NETWORK_CONNECT) { Toast.makeText( MyApplication.getInstance().getApplicationContext(), "您的網(wǎng)絡(luò)出錯(cuò)啦!", Toast.LENGTH_LONG).show(); } else if (iError == MKEvent.ERROR_NETWORK_DATA) { Toast.makeText( MyApplication.getInstance().getApplicationContext(), "輸入正確的檢索條件!", Toast.LENGTH_LONG).show(); } // ... } public void onGetPermissionState(int iError) { if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授權(quán)Key錯(cuò)誤: Toast.makeText( MyApplication.getInstance().getApplicationContext(), "請(qǐng)輸入正確的授權(quán)Key!", Toast.LENGTH_LONG).show(); MyApplication.getInstance().m_bKeyRight = false; } } } }
三、編寫(xiě)主程序MainActivity,顯示當(dāng)前所處位置
主程序MainActity:
public class MainActivity extends Activity { public MyApplication app; static MapView mMapView = null; public MKMapViewListener mMapListener = null; MyLocationOverlay myLocationOverlay = null; // 定位相關(guān) LocationClient mLocClient; public NotifyLister mNotifyer = null; public MyLocationListenner myListener = new MyLocationListenner(); LocationData locData = null; private MapController mMapController = null; static MKSearch mkSerach; Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, "msg:" + msg.what, Toast.LENGTH_SHORT).show(); }; }; static TextView showAddr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showAddr = (TextView) findViewById(R.id.showAddr); MyIcon mi = new MyIcon(this); //在屏幕中心點(diǎn)添加接我圖標(biāo) getWindow().addContentView( mi, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); MyIcon2 m2 = new MyIcon2(this); getWindow().addContentView( m2, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); mMapView = (MapView) findViewById(R.id.bmapsView); mMapController = mMapView.getController(); initMapView(); app = MyApplication.getInstance(); mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); //搜索初始化 mkSerach = new MKSearch(); mkSerach.init(app.mBMapManager, new MKSearchListener() { @Override public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onGetPoiDetailSearchResult(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetAddrResult(MKAddrInfo info, int arg1) { showAddr.setText(info.strAddr); } }); //設(shè)置地圖相關(guān) LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setCoorType("bd09ll"); option.setScanSpan(300000); mLocClient.setLocOption(option); mLocClient.start(); mMapView.getController().setZoom(16); mMapView.getController().enableClick(true); mMapView.displayZoomControls(true); mMapListener = new MKMapViewListener() { public void onMapMoveFinish() { } public void onClickMapPoi(MapPoi mapPoiInfo) { // TODO Auto-generated method stub String title = ""; if (mapPoiInfo != null) { title = mapPoiInfo.strText; Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT) .show(); } } }; mMapView.regMapViewListener(MyApplication.getInstance().mBMapManager, mMapListener); myLocationOverlay = new MyLocationOverlay(mMapView); locData = new LocationData(); myLocationOverlay.setData(locData); mMapView.getOverlays().add(myLocationOverlay); myLocationOverlay.enableCompass(); mMapView.refresh(); } private void initMapView() { mMapView.setLongClickable(true); } /** * 監(jiān)聽(tīng)函數(shù),又新位置的時(shí)候,格式化成字符串,輸出到屏幕中 */ public class MyLocationListenner implements BDLocationListener { public void onReceiveLocation(BDLocation location) { if (location == null) return; locData.latitude = location.getLatitude(); locData.longitude = location.getLongitude(); locData.direction = 2.0f; locData.accuracy = location.getRadius(); locData.direction = location.getDerect(); Log.d("loctest", String.format("before: lat: %f lon: %f", location.getLatitude(), location.getLongitude())); myLocationOverlay.setData(locData); mMapView.refresh(); mMapController .animateTo(new GeoPoint((int) (locData.latitude * 1e6), (int) (locData.longitude * 1e6)), mHandler .obtainMessage(1)); } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null) { return; } } } public class NotifyLister extends BDNotifyListener { public void onNotify(BDLocation mlocation, float distance) { } } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); mMapView.onRestoreInstanceState(savedInstanceState); } public static void getPosition(GeoPoint g) { mkSerach.reverseGeocode(g); showAddr.setText("獲取位置中..."); } }
Xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/showAddr" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="選擇地點(diǎn)" android:textSize="20dp" /> <com.baidu.mapapi.map.MapView android:id="@+id/bmapsView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
三、繪制圖形
MyIcon 的onDraw實(shí)現(xiàn)繪制中心點(diǎn)的圖標(biāo),MyIcon2繪制指示器圖標(biāo),如圖所示
public class MyIcon extends View { public static int w; public static int h; public static Bitmap mBitmap; public MyIcon(Context context) { super(context); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.me); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); w = this.getWidth() / 2 - mBitmap.getWidth() / 2; h = this.getHeight() / 2 - mBitmap.getHeight() / 2; canvas.drawBitmap(mBitmap, w, h, null); } } public class MyIcon2 extends View{ public static int w; public static int h; private Bitmap mBitmap; public MyIcon2(Context context) { super(context); mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.jiewo); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); w = this.getWidth() / 2 - mBitmap.getWidth() / 2; h = (this.getHeight() / 2 - mBitmap.getHeight() / 2) - ( MyIcon.mBitmap.getHeight()/2); canvas.drawBitmap(mBitmap, w, h, null); } }
以上所述是小編給大家分享Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位的相關(guān)知識(shí),希望對(duì)大家有所幫助。
- android實(shí)現(xiàn)百度地圖自定義彈出窗口功能
- Android百度地圖定位后獲取周邊位置的實(shí)現(xiàn)代碼
- Android下如何使用百度地圖sdk
- 基于Android實(shí)現(xiàn)百度地圖定位過(guò)程詳解
- Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
- Android 百度地圖POI搜索功能實(shí)例代碼
- Android 調(diào)用百度地圖API示例
- Android百度地圖自定義公交路線(xiàn)導(dǎo)航
- Android SDK 百度地圖通過(guò)poi城市內(nèi)檢索簡(jiǎn)介接口的使用
- Android百度地圖之方向感應(yīng)和模式更改
相關(guān)文章
Android獲取驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android獲取驗(yàn)證碼倒計(jì)時(shí)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()
這篇文章主要給大家介紹了關(guān)于Android中兩個(gè)類(lèi)讓你再也不用實(shí)現(xiàn)onActivityResult()的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-08-08深入Android Handler,MessageQueue與Looper關(guān)系
這篇文章主要介紹了深入Android Handler,MessageQueue與Looper關(guān)系,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android開(kāi)發(fā)graphics?bufferqueue整體流程
這篇文章主要為大家介紹了Android開(kāi)發(fā)graphics?bufferqueue整體流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07