您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android中怎么接入百度地圖定位,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
1、構(gòu)建定位Option的工具類(lèi)
import com.baidu.location.LocationClientOption; public class LocationClientOptionBuilder { private LocationClientOption option; public static LocationClientOptionBuilder builder() { LocationClientOptionBuilder builder = new LocationClientOptionBuilder(); builder.option = builder.initOption(); return builder; } /** * 設(shè)置坐標(biāo)系 * * @return * @see CoorType */ public LocationClientOptionBuilder setCoorType() { return setCoorType(CoorType.bd09ll); } public LocationClientOptionBuilder setCoorType(CoorType coorType) { this.option.setCoorType(coorType.name()); return this; } /** * 連續(xù)定位 * 可選,設(shè)置發(fā)起定位請(qǐng)求的間隔,int類(lèi)型,單位ms * 如果設(shè)置為0,則代表單次定位,即僅定位一次,默認(rèn)為0 * 如果設(shè)置非0,需設(shè)置1000ms以上才有效 * * @param time * @return */ public LocationClientOptionBuilder setScanSpan(int time) { this.option.setScanSpan(time); return this; } public LocationClientOption bulid() { return this.option; } private LocationClientOption initOption() { LocationClientOption option = new LocationClientOption(); //可選,設(shè)置定位模式,默認(rèn)高精度 //LocationMode.Hight_Accuracy:高精度; //LocationMode. Battery_Saving:低功耗; //LocationMode. Device_Sensors:僅使用設(shè)備; option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); //可選,設(shè)置返回經(jīng)緯度坐標(biāo)類(lèi)型,默認(rèn)GCJ02 //GCJ02:國(guó)測(cè)局坐標(biāo); //BD09ll:百度經(jīng)緯度坐標(biāo); //BD09:百度墨卡托坐標(biāo); //海外地區(qū)定位,無(wú)需設(shè)置坐標(biāo)類(lèi)型,統(tǒng)一返回WGS84類(lèi)型坐標(biāo) // option.setCoorType("bd09ll"); //可選,設(shè)置發(fā)起定位請(qǐng)求的間隔,int類(lèi)型,單位ms //如果設(shè)置為0,則代表單次定位,即僅定位一次,默認(rèn)為0 //如果設(shè)置非0,需設(shè)置1000ms以上才有效 // option.setScanSpan(1000); //可選,設(shè)置是否使用gps,默認(rèn)false //使用高精度和僅用設(shè)備兩種定位模式的,參數(shù)必須設(shè)置為true option.setOpenGps(true); //可選,設(shè)置是否當(dāng)GPS有效時(shí)按照1S/1次頻率輸出GPS結(jié)果,默認(rèn)false // option.setLocationNotify(true); //可選,定位SDK內(nèi)部是一個(gè)service,并放到了獨(dú)立進(jìn)程。 //設(shè)置是否在stop的時(shí)候殺死這個(gè)進(jìn)程,默認(rèn)(建議)不殺死,即setIgnoreKillProcess(true) // option.setIgnoreKillProcess(true); //可選,設(shè)置是否收集Crash信息,默認(rèn)收集,即參數(shù)為false // option.SetIgnoreCacheException(false); //可選,V7.2版本新增能力 //如果設(shè)置了該接口,首次啟動(dòng)定位時(shí),會(huì)先判斷當(dāng)前Wi-Fi是否超出有效期,若超出有效期,會(huì)先重新掃描Wi-Fi,然后定位 // option.setWifiCacheTimeOut(5 * 60 * 1000); //可選,設(shè)置是否需要過(guò)濾GPS仿真結(jié)果,默認(rèn)需要,即參數(shù)為false // option.setEnableSimulateGps(false); return option; } /** * 坐標(biāo)系 */ public enum CoorType { gcj02, bd09, bd09ll } }
2、構(gòu)建定位的工具類(lèi)
import android.content.Context; import android.support.annotation.NonNull; import android.util.Log; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; /** * 百度地圖定位工具類(lèi) * * @author peter 2018-12-21 10:12 */ public class BMapLocationHelper { private static final int LOCATION_SUCCESS = 1; static final int LOCATION_FAIL = -1; private LocationClient mLocationClient; private MyLocationListener myListener = new MyLocationListener(); private LocationCallBack callBack; private BMapLocationHelper(LocationCallBack callBack) { this.callBack = callBack; } public static BMapLocationHelper create(@NonNull Context context, @NonNull LocationClientOption option, @NonNull LocationCallBack callBack) { BMapLocationHelper bMapLocationHelper = new BMapLocationHelper(callBack); LocationClient client = new LocationClient(context); client.setLocOption(option); //聲明LocationClient類(lèi) client.registerLocationListener(bMapLocationHelper.myListener); bMapLocationHelper.mLocationClient = client; return bMapLocationHelper; } /** * 開(kāi)始定位 */ public void locStart() { mLocationClient.start(); } /** * 停止定位 */ public void locStop() { mLocationClient.stop(); } public void locReStart() { mLocationClient.restart(); } public LocationClient getmLocationClient() { return mLocationClient; } /** * 地圖定位結(jié)果監(jiān)聽(tīng)類(lèi) */ public class MyLocationListener extends BDAbstractLocationListener { private static final String TAG = "MyLocationListener"; @Override public void onReceiveLocation(BDLocation location) { if (location == null) return; int locType = location.getLocType(); int status = LOCATION_SUCCESS; if (locType != 61 && locType != 161 && locType != 66) status = LOCATION_FAIL; String errMsg = getLocationResultMsg(locType); callBack.onReceiveLocation(status, location, errMsg); } @Override public void onLocDiagnosticMessage(int i, int i1, String s) { Log.i(TAG, "onLocDiagnosticMessage: " + i + "diaType:" + i1); callBack.onLocDiagnosticMessage(i, i1, getLocDiagnosticMessage(i, i1)); super.onLocDiagnosticMessage(i, i1, s); } } /** * 回調(diào)類(lèi) */ public abstract static class LocationCallBack { /** * 定位的結(jié)果 * * @param statusCode 狀態(tài)碼,1:定位成功,-1定位失敗 * @param bdLocation 定位成功時(shí)返回的定位結(jié)果對(duì)象 * @param errMsg 定位失敗時(shí)的錯(cuò)誤信息,成功時(shí)則為null */ public abstract void onReceiveLocation(int statusCode, BDLocation bdLocation, String errMsg); /** * 錯(cuò)誤的狀態(tài)碼 * <a>http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/addition-func/error-code</a> * <p> * 回調(diào)定位診斷信息,開(kāi)發(fā)者可以根據(jù)相關(guān)信息解決定位遇到的一些問(wèn)題 * * @param locType 當(dāng)前定位類(lèi)型 * @param diagnosticType 診斷類(lèi)型(1~9) * @param diagnosticMessage 具體的診斷信息釋義 */ public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) { } } /** * 錯(cuò)誤的狀態(tài)碼 * <a>http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/addition-func/error-code</a> * * @param locType 當(dāng)前定位類(lèi)型 * @return String 定位成功或失敗的信息 */ private String getLocationResultMsg(int locType) { switch (locType) { case 61: return "GPS定位結(jié)果,GPS定位成功"; case 62: return "無(wú)法獲取有效定位依據(jù),定位失敗,請(qǐng)檢查運(yùn)營(yíng)商網(wǎng)絡(luò)或者WiFi網(wǎng)絡(luò)是否正常開(kāi)啟,嘗試重新請(qǐng)求定位"; case 63: return "網(wǎng)絡(luò)異常,沒(méi)有成功向服務(wù)器發(fā)起請(qǐng)求,請(qǐng)確認(rèn)當(dāng)前測(cè)試手機(jī)網(wǎng)絡(luò)是否通暢,嘗試重新請(qǐng)求定位"; case 66: return "離線定位結(jié)果。通過(guò)requestOfflineLocaiton調(diào)用時(shí)對(duì)應(yīng)的返回結(jié)果"; case 67: return "離線定位失敗"; case 161: return "網(wǎng)絡(luò)定位結(jié)果,網(wǎng)絡(luò)定位成功"; case 162: return "請(qǐng)求串密文解析失敗,一般是由于客戶端SO文件加載失敗造成,請(qǐng)嚴(yán)格參照開(kāi)發(fā)指南或demo開(kāi)發(fā),放入對(duì)應(yīng)SO文件"; case 167: return "服務(wù)端定位失敗,請(qǐng)您檢查是否禁用獲取位置信息權(quán)限,嘗試重新請(qǐng)求定位"; case 505: return "AK不存在或者非法,請(qǐng)按照說(shuō)明文檔重新申請(qǐng)AK"; default: return ""; } } /** * @param locType 當(dāng)前定位類(lèi)型 * @param diagnosticType 診斷類(lèi)型(1~9) * @return String */ private String getLocDiagnosticMessage(int locType, int diagnosticType) { switch (locType) { case 62: switch (diagnosticType) { case 4: return "定位失敗,無(wú)法獲取任何有效定位依據(jù)"; case 5: return "定位失敗,無(wú)法獲取有效定位依據(jù),請(qǐng)檢查運(yùn)營(yíng)商網(wǎng)絡(luò)或者Wi-Fi網(wǎng)絡(luò)是否正常開(kāi)啟,嘗試重新請(qǐng)求定位"; case 6: return "定位失敗,無(wú)法獲取有效定位依據(jù),請(qǐng)嘗試插入一張sim卡或打開(kāi)Wi-Fi重試"; case 7: return "定位失敗,飛行模式下無(wú)法獲取有效定位依據(jù),請(qǐng)關(guān)閉飛行模式重試"; case 9: return "定位失敗,無(wú)法獲取任何有效定位依據(jù)"; } case 67: if (diagnosticType == 3) return "定位失敗,請(qǐng)您檢查您的網(wǎng)絡(luò)狀態(tài)"; case 161: switch (diagnosticType) { case 1: return "定位失敗,建議您打開(kāi)GPS"; case 2: return "定位失敗,建議您打開(kāi)Wi-Fi"; } case 167: if (diagnosticType == 8) return "定位失敗,請(qǐng)確認(rèn)您定位的開(kāi)關(guān)打開(kāi)狀態(tài),是否賦予APP定位權(quán)限"; default: return "未知錯(cuò)誤"; } } }
3、顯示用戶當(dāng)前位置到地圖的工具類(lèi)
import android.content.Context; import android.support.annotation.NonNull; import android.util.Log; import com.baidu.location.BDLocation; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.yikesong.sender.util.ToastUtils; /** * 顯示用戶當(dāng)前位置到地圖上 * * @author peter 2018-12-21 16:27 */ public class UserLocation extends BMapLocationHelper.LocationCallBack { private Context context; private boolean isFirstLoc = true; private BaiduMap map; private int mCurrentDirection = 0; private BMapLocationHelper helper; private static final String TAG = "UserLocation"; public UserLocation(@NonNull Context context, @NonNull BaiduMap map) { this.context = context; this.map = map; init(); } private void init() { map.setMapType(BaiduMap.MAP_TYPE_NORMAL); //開(kāi)啟定位圖層 map.setMyLocationEnabled(true); } /** * 在地圖上顯示用戶的當(dāng)前位置 */ public void showUserLocationOnMap() { if (helper == null) { LocationClientOption option = LocationClientOptionBuilder .builder() .setCoorType() .bulid(); helper = BMapLocationHelper.create(this.context, option, this); } helper.locStart(); } @Override public void onReceiveLocation(int statusCode, BDLocation bdLocation, String errMsg) { if (statusCode == BMapLocationHelper.LOCATION_FAIL) { ToastUtils.toastInfo(errMsg, context); Log.i(TAG, "onReceiveLocation: " + errMsg); return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(bdLocation.getRadius()) // 此處設(shè)置開(kāi)發(fā)者獲取到的方向信息,順時(shí)針0-360 .direction(mCurrentDirection).latitude(bdLocation.getLatitude()) .longitude(bdLocation.getLongitude()).build(); map.setMyLocationData(locData); if (isFirstLoc) { isFirstLoc = false; LatLng centerPoint = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()); MapStatus mapStatus = new MapStatus.Builder() .target(centerPoint) //設(shè)置中心點(diǎn) .zoom(18f)//設(shè)置縮放級(jí)別 .build(); map.animateMapStatus(MapStatusUpdateFactory.newMapStatus(mapStatus)); } if (helper != null) helper.locStop(); } @Override public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) { super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage); } public BMapLocationHelper getHelper() { return helper; } public BaiduMap getMap() { return map; } public int getmCurrentDirection() { return mCurrentDirection; } }
關(guān)于Android中怎么接入百度地圖定位就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。