導讀跟大家講解下有關百度LBS定位怎么用,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說百度LBS定位怎么用,小編也收集到了有關
跟大家講解下有關百度LBS定位怎么用,相信小伙伴們對這個話題應該也很關注吧,現在就為小伙伴們說說百度LBS定位怎么用,小編也收集到了有關百度LBS定位怎么用的相關資料,希望大家看到了會喜歡。
百度LBS定位怎么用?
up主最近在寫一個新項目,關于借款的一個app,正好更具需求用到了定位,其實對于一般程序員來說,定位可能是最基本的了吧,之前我在項目中用到了高德定位,這次根據需求選擇了百度定位,話不多說,直接走起;
1.在Application標簽中聲明SERVICE組件。
2.聲明使用權限,這里復制Api文檔內容即可:
3.那就開始配置你的appkey了;
4.我是寫到一個工具類里邊在MainActivity里邊oncreat方法下添加這樣的一句話即可;
5.最后我們在log日志里看一下,可以在把全部位置信息傳給后臺,這里我需要的就是一個city顯示。
6。這樣的就完成了一個定位,這是第一次寫,邏輯一定是對的,可能會表述不太明白,我會繼續努力的,感謝!!!
備注:以下是我的代碼
public classMyLocationUtil {public staticLocationClientmLocationClient=null;public staticBDLocationListenermyListener=newMyLocationListener();public static voidgetLocation(Context context) {mLocationClient=newLocationClient(context);//聲明LocationClient類mLocationClient.registerLocationListener(myListener);//注冊監聽函數initLocation();mLocationClient.start();}/***設置定位參數*高精度定位模式:這種定位模式下,會同時使用網絡定位和GPS定位,優先返回最高精度的定位結果;低功耗定位模式:這種定位模式下,不會使用GPS進行定位,只會使用網絡定位(WiFi定位和基站定位);僅用設備定位模式:這種定位模式下,不需要連接網絡,只使用GPS進行定位,這種模式下不支持室內環境的定位。*/private static voidinitLocation(){LocationClientOption option =newLocationClientOption();option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可選,默認高精度,設置定位模式,高精度,低功耗,僅設備option.setCoorType("bd09ll");//可選,默認gcj02,設置返回的定位結果坐標系intspan=1000;option.setScanSpan(0);//可選,默認0,即僅定位一次,設置發起定位請求的間隔需要大于等于1000ms才是有效的option.setIsNeedAddress(true);//可選,設置是否需要地址信息,默認不需要option.setOpenGps(true);//可選,默認false,設置是否使用gpsoption.setLocationNotify(true);//可選,默認false,設置是否當GPS有效時按照1S/1次頻率輸出GPS結果option.setIsNeedLocationDescribe(true);//可選,默認false,設置是否需要位置語義化結果,可以在BDLocation.getLocationDescribe里得到,結果類似于“在北京天安門附近”option.setIsNeedLocationPoiList(true);//可選,默認false,設置是否需要POI結果,可以在BDLocation.getPoiList里得到option.setIgnoreKillProcess(false);//可選,默認true,定位SDK內部是一個SERVICE,并放到了獨立進程,設置是否在stop的時候殺死這個進程,默認不殺死option.SetIgnoreCacheException(false);//可選,默認false,設置是否收集CRASH信息,默認收集option.setEnableSimulateGps(false);//可選,默認false,設置是否需要過濾GPS仿真結果,默認需要mLocationClient.setLocOption(option);}public static classMyLocationListenerimplementsBDLocationListener {@Overridepublic voidonReceiveLocation(BDLocation location) {mLocationClient.stop();//Receive LocationStringBuffer sb =newStringBuffer(256);sb.append("periods : ");sb.append(location.getTime());sb.append("\nerror code : ");sb.append(location.getLocType());sb.append("\nlatitude : ");sb.append(location.getLatitude());sb.append("\nlontitude : ");sb.append(location.getLongitude());sb.append("\nradius : ");sb.append(location.getRadius());if(location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位結果sb.append("\nspeed : ");sb.append(location.getSpeed());//單位:公里每小時sb.append("\nsatellite : ");sb.append(location.getSatelliteNumber());sb.append("\nheight : ");sb.append(location.getAltitude());//單位:米sb.append("\ndirection : ");sb.append(location.getDirection());//單位度sb.append("\naddr : ");sb.append(location.getAddrStr());sb.append("\ndescribe : ");sb.append("gps定位成功");}else if(location.getLocType() == BDLocation.TypeNetWorkLocation){//網絡定位結果sb.append("\naddr : ");sb.append(location.getAddrStr());sb.append("\ncity:");sb.append(location.getCity());//運營商信息sb.append("\noperationers : ");sb.append(location.getOperators());sb.append("\ndescribe : ");sb.append("網絡定位成功");}else if(location.getLocType() == BDLocation.TypeOffLineLocation) {//離線定位結果sb.append("\ndescribe : ");sb.append("離線定位成功,離線定位結果也是有效的");}else if(location.getLocType() == BDLocation.TypeServerError) {sb.append("\ndescribe : ");sb.append("服務端網絡定位失敗,可以反饋IMEI號和大體定位時間到loc-bugs@baidu.com,會有人追查原因");}else if(location.getLocType() == BDLocation.TypeNetWorkException) {sb.append("\ndescribe : ");sb.append("網絡不同導致定位失敗,請檢查網絡是否通暢");}else if(location.getLocType() == BDLocation.TypeCriteriaException) {sb.append("\ndescribe : ");sb.append("無法獲取有效定位依據導致定位失敗,一般是由于手機的原因,處于飛行模式下一般會造成這種結果,可以試著重啟手機");}sb.append("\nlocationdescribe : ");sb.append(location.getLocationDescribe());//位置語義化信息List list = location.getPoiList();// POI數據if(list !=null) {sb.append("\npoilist size = : ");sb.append(list.size());for(Poi p : list) {sb.append("\npoi= : ");sb.append(p.getId() +" "+ p.getName() +" "+ p.getRank());}}Log.i("yunxiao",sb.toString());}}}更多相關知識,請訪問PHP中文網!
來源:php中文網