1. 程式人生 > >使用百度地圖api實現定位及選擇位置功能

使用百度地圖api實現定位及選擇位置功能

之前使用過一些第三方的地圖api,最後還是覺得百度的比較好用,剛好最近做一個專案,需要用到地圖的功能,也比較簡單,就是獲取當前位置,然後可以自己手動在地圖上選擇位置,把經緯度丟給服務端。我用的就是百度地圖

下面就開始做了,註冊開發者賬號、申請key、下載SDK什麼的,就不說了,官網上說的很清楚。就直接貼程式碼了:

我先建了一個類,繼承Application,裡面就一行程式碼,初始化地圖。

  1. publicclass LocationApp extends Application {  
  2.     @Override
  3.     publicvoid onCreate() {  
  4.         super
    .onCreate();  
  5.         SDKInitializer.initialize(getApplicationContext());  
  6.     }  
  7. }  

為什麼地圖初始化的方法要寫這裡,因為文件裡做了說明:


然後就是主類了,裡面我先貼一部分程式碼,完整的之後在文章最後面有下載連結。

首先要在main.xml裡面把地圖的控制元件給加上:

  1. <com.baidu.mapapi.map.MapView
  2.         android:id="@+id/bmapView"
  3.         android:layout_width="match_parent"
  4.         android:layout_height="match_parent"
  5.         android:clickable="true"/>

然後在Main.java中,對這個控制元件進行一些初始化,就可以展示一張最基本的地圖了。
  1. private MapView mapView;  
  2. private BaiduMap baiduMap;  

  1. mapView = (MapView) findViewById(R.id.bmapView);  
  2. baiduMap = mapView.getMap();  
  3. baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(17
    ));//地圖縮放級別為17

就這樣就可以了,執行的話,應該就可以看到位置在北京的地圖。

當然這只是最簡單了,還要有定位的功能,定位出來後,要在地圖上把這個點標出來,然後把地圖移動到這個點的位置。

  1. private LocationClient locationClient;  
  2. private BDLocationListener listener = new MyLocationListener();  
  1. locationClient = new LocationClient(getApplicationContext());  
  2. locationClient.registerLocationListener(listener);  
  3. initLocation();  
  1. // 初始化定位引數
  2. privatevoid initLocation(){  
  3.         LocationClientOption option = new LocationClientOption();  
  4.         option.setCoorType("bd09ll");//可選,預設gcj02,設定返回的定位結果座標系
  5.         int span=1000;  
  6.         option.setScanSpan(span);//可選,預設0,即僅定位一次,設定發起定位請求的間隔需要大於等於1000ms才是有效的
  7.         option.setIsNeedAddress(true);//可選,設定是否需要地址資訊,預設不需要
  8.         option.setOpenGps(true);//可選,預設false,設定是否使用gps
  9.         option.setLocationNotify(true);//可選,預設false,設定是否當gps有效時按照1S1次頻率輸出GPS結果
  10.         option.setIsNeedLocationDescribe(true);//可選,預設false,設定是否需要位置語義化結果
  11.         option.setIsNeedLocationPoiList(true);//可選,預設false,設定是否需要POI結果
  12.         option.setIgnoreKillProcess(false);//可選,預設false,定位SDK內部是一個SERVICE,並放到了獨立程序,設定是否在stop的時候殺死這個程序,預設殺死
  13.         option.SetIgnoreCacheException(false);//可選,預設false,設定是否收集CRASH資訊,預設收集
  14.         option.setEnableSimulateGps(false);//可選,預設false,設定是否需要過濾gps模擬結果,預設需要
  15.         locationClient.setLocOption(option);  
  16.         locationClient.start();  
  17.     }  
  1. // 定位監聽
  2. publicclass MyLocationListener implements BDLocationListener {  
  3.     @Override
  4.     publicvoid onReceiveLocation(BDLocation loc) {  
  5.         if (loc != null && (loc.getLocType() == 161 || loc.getLocType() == 66)) {  
  6.             //這裡得到BDLocation就是定位出來的資訊了
  7.         } else {  
  8.             Toast.makeText(MainActivity.this"定位失敗,請檢查手機網路或設定!", Toast.LENGTH_LONG).show();  
  9.         }  
  10.     }  
  11. }  

上面的這些程式碼,就是定位的程式碼了,在MyLocationListener裡面,BDLocation有值的話,就說明定位成功了。下面附上BDLocation的說明,想得到什麼資訊,都可以去得到:

內容就不貼了,太長了,直接給個網站,開啟就可以了:百度API

既然定位的資訊得到了,就要在地圖上添加個標註,顯示當前位置:

  1. LatLng point = new LatLng(location.getLatitude(), location.getLongitude());  
  2. setMapOverlay(point);  
  3. baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));  
  1. // 在地圖上新增標註
  2. privatevoid setMapOverlay(LatLng point) {  
  3.     latitude = point.latitude;  
  4.     longitude = point.longitude;  
  5.     baiduMap.clear();  
  6.     BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_openmap_mark);  
  7.     OverlayOptions option = new MarkerOptions().position(point).icon(bitmap);  
  8.     baiduMap.addOverlay(option);  
  9. }  

這樣就可以了,顯示的結果就是下面這樣的:


這樣就滿足了第一個需求,就是獲取當前位置,並在地圖上顯示出來。第二個需求就是要能自己在地圖上選點,比如現在自動定的位置,不想讓服務端知道在這裡,要換一個,就可以把地圖拖動到其他地方,選擇自己想要的位置,點一下,就可以了。

這個功能實現起來也比較簡單,給地圖新增一個OnMapClickListener點選事件就可以了:

  1. baiduMap.setOnMapClickListener(new OnMapClickListener() {  
  2.         @Override
  3.         publicboolean onMapPoiClick(MapPoi poi) {  
  4.             returnfalse;  
  5.         }  
  6.         @Override
  7.         publicvoid onMapClick(LatLng point) {  
  8.             Log.d("map click", point.longitude+","+point.latitude);  
  9.             setMapOverlay(point);  
  10.             getInfoFromLAL(point);  
  11.         }  
  12. });  
  1. // 根據經緯度查詢位置
  2.     privatevoid getInfoFromLAL(final LatLng point) {  
  3.         pb.setVisibility(View.VISIBLE);  
  4.         infoText.setText("經度:"+point.latitudeE6+",緯度"+point.latitudeE6);  
  5.         GeoCoder gc = GeoCoder.newInstance();  
  6.         gc.reverseGeoCode(new ReverseGeoCodeOption().location(point));  
  7.         gc.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {  
  8.             @Override
  9.             publicvoid onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {  
  10.                 pb.setVisibility(View.GONE);  
  11.                 if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {  
  12.                     Log.e("發起反地理編碼請求""未能找到結果");  
  13.                 } else {  
  14.                     infoText.setText("經度:"+point.latitudeE6+",緯度"+point.latitudeE6  
  15.                             +"\n"+result.getAddress());  
  16.                 }  
  17.             }  
  18.             @Override
  19.             publicvoid onGetGeoCodeResult(GeoCodeResult result) {  
  20.             }  
  21.         });  
  22.     }  

這樣就OK了。選擇其他地方的一個點,就可以得到該點的位置了:

完事了,功能比較簡單,附上原始碼下載地址:csdn下載

專案放到eclipse中後,一定要在AndroidManifest.xml中把自己申請的key加上。