使用百度地圖api實現定位及選擇位置功能
阿新 • • 發佈:2019-01-29
之前使用過一些第三方的地圖api,最後還是覺得百度的比較好用,剛好最近做一個專案,需要用到地圖的功能,也比較簡單,就是獲取當前位置,然後可以自己手動在地圖上選擇位置,把經緯度丟給服務端。我用的就是百度地圖
下面就開始做了,註冊開發者賬號、申請key、下載SDK什麼的,就不說了,官網上說的很清楚。就直接貼程式碼了:
我先建了一個類,繼承Application,裡面就一行程式碼,初始化地圖。
- publicclass LocationApp extends Application {
- @Override
- publicvoid onCreate() {
-
super
- SDKInitializer.initialize(getApplicationContext());
- }
- }
為什麼地圖初始化的方法要寫這裡,因為文件裡做了說明:
然後就是主類了,裡面我先貼一部分程式碼,完整的之後在文章最後面有下載連結。
首先要在main.xml裡面把地圖的控制元件給加上:
- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
-
android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:clickable="true"/>
然後在Main.java中,對這個控制元件進行一些初始化,就可以展示一張最基本的地圖了。
- private MapView mapView;
- private BaiduMap baiduMap;
- mapView = (MapView) findViewById(R.id.bmapView);
- baiduMap = mapView.getMap();
-
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(17
就這樣就可以了,執行的話,應該就可以看到位置在北京的地圖。
當然這只是最簡單了,還要有定位的功能,定位出來後,要在地圖上把這個點標出來,然後把地圖移動到這個點的位置。
- private LocationClient locationClient;
- private BDLocationListener listener = new MyLocationListener();
- locationClient = new LocationClient(getApplicationContext());
- locationClient.registerLocationListener(listener);
- initLocation();
- // 初始化定位引數
- privatevoid initLocation(){
- LocationClientOption option = new LocationClientOption();
- option.setCoorType("bd09ll");//可選,預設gcj02,設定返回的定位結果座標系
- int span=1000;
- option.setScanSpan(span);//可選,預設0,即僅定位一次,設定發起定位請求的間隔需要大於等於1000ms才是有效的
- option.setIsNeedAddress(true);//可選,設定是否需要地址資訊,預設不需要
- option.setOpenGps(true);//可選,預設false,設定是否使用gps
- option.setLocationNotify(true);//可選,預設false,設定是否當gps有效時按照1S1次頻率輸出GPS結果
- option.setIsNeedLocationDescribe(true);//可選,預設false,設定是否需要位置語義化結果
- option.setIsNeedLocationPoiList(true);//可選,預設false,設定是否需要POI結果
- option.setIgnoreKillProcess(false);//可選,預設false,定位SDK內部是一個SERVICE,並放到了獨立程序,設定是否在stop的時候殺死這個程序,預設殺死
- option.SetIgnoreCacheException(false);//可選,預設false,設定是否收集CRASH資訊,預設收集
- option.setEnableSimulateGps(false);//可選,預設false,設定是否需要過濾gps模擬結果,預設需要
- locationClient.setLocOption(option);
- locationClient.start();
- }
- // 定位監聽
- publicclass MyLocationListener implements BDLocationListener {
- @Override
- publicvoid onReceiveLocation(BDLocation loc) {
- if (loc != null && (loc.getLocType() == 161 || loc.getLocType() == 66)) {
- //這裡得到BDLocation就是定位出來的資訊了
- } else {
- Toast.makeText(MainActivity.this, "定位失敗,請檢查手機網路或設定!", Toast.LENGTH_LONG).show();
- }
- }
- }
上面的這些程式碼,就是定位的程式碼了,在MyLocationListener裡面,BDLocation有值的話,就說明定位成功了。下面附上BDLocation的說明,想得到什麼資訊,都可以去得到:
內容就不貼了,太長了,直接給個網站,開啟就可以了:百度API
既然定位的資訊得到了,就要在地圖上添加個標註,顯示當前位置:
- LatLng point = new LatLng(location.getLatitude(), location.getLongitude());
- setMapOverlay(point);
- baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));
- // 在地圖上新增標註
- privatevoid setMapOverlay(LatLng point) {
- latitude = point.latitude;
- longitude = point.longitude;
- baiduMap.clear();
- BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_openmap_mark);
- OverlayOptions option = new MarkerOptions().position(point).icon(bitmap);
- baiduMap.addOverlay(option);
- }
這樣就可以了,顯示的結果就是下面這樣的:
這樣就滿足了第一個需求,就是獲取當前位置,並在地圖上顯示出來。第二個需求就是要能自己在地圖上選點,比如現在自動定的位置,不想讓服務端知道在這裡,要換一個,就可以把地圖拖動到其他地方,選擇自己想要的位置,點一下,就可以了。
這個功能實現起來也比較簡單,給地圖新增一個OnMapClickListener點選事件就可以了:
- baiduMap.setOnMapClickListener(new OnMapClickListener() {
- @Override
- publicboolean onMapPoiClick(MapPoi poi) {
- returnfalse;
- }
- @Override
- publicvoid onMapClick(LatLng point) {
- Log.d("map click", point.longitude+","+point.latitude);
- setMapOverlay(point);
- getInfoFromLAL(point);
- }
- });
- // 根據經緯度查詢位置
- privatevoid getInfoFromLAL(final LatLng point) {
- pb.setVisibility(View.VISIBLE);
- infoText.setText("經度:"+point.latitudeE6+",緯度"+point.latitudeE6);
- GeoCoder gc = GeoCoder.newInstance();
- gc.reverseGeoCode(new ReverseGeoCodeOption().location(point));
- gc.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
- @Override
- publicvoid onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
- pb.setVisibility(View.GONE);
- if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
- Log.e("發起反地理編碼請求", "未能找到結果");
- } else {
- infoText.setText("經度:"+point.latitudeE6+",緯度"+point.latitudeE6
- +"\n"+result.getAddress());
- }
- }
- @Override
- publicvoid onGetGeoCodeResult(GeoCodeResult result) {
- }
- });
- }
這樣就OK了。選擇其他地方的一個點,就可以得到該點的位置了:
完事了,功能比較簡單,附上原始碼下載地址:csdn下載
專案放到eclipse中後,一定要在AndroidManifest.xml中把自己申請的key加上。