當前位置:
首頁 >
百度地图API使用之实现定位
發(fā)布時間:2025/6/17
43
豆豆
生活随笔
收集整理的這篇文章主要介紹了
百度地图API使用之实现定位
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、初始化LocationClient類
/** 此處需要注意:LocationClient類必須在主線程中聲明。需要Context類型的參數(shù)。* Context需要時全進程有效的context,推薦用getApplicationConext獲取全進程有效的context*/public LocationClient mLocationClient = null;// BDLocationListener處理定位結(jié)果// MyLocationListener實現(xiàn)兩個方法:定位請求回調(diào)函數(shù)+poi請求回調(diào)函數(shù)public BDLocationListener myListener = new MyLocationListener();public void onCreate() {mLocationClient = new LocationClient(getApplicationContext()); //聲明LocationClient類mLocationClient.registerLocationListener( myListener ); //注冊監(jiān)聽函數(shù) }?
2、實現(xiàn)BDLocationListener接口
/*** @author JL BDLocationListener接口有2個方法需要實現(xiàn):* 1.接收異步返回的定位結(jié)果,參數(shù)是BDLocation類型參數(shù)。* 2.接收異步返回的POI查詢結(jié)果,參數(shù)是BDLocation類型參數(shù)。*/public class MyLocationListener implements BDLocationListener {/** 接收異步返回的定位結(jié)果 BDLocation包含詳細的定位信息*/@Overridepublic void onReceiveLocation(BDLocation location) {// TODO Auto-generated method stubif (location == null)return;StringBuffer sb = new StringBuffer(256);sb.append("當前定位時間 : ");sb.append(location.getTime());sb.append("\n獲取定位類型 : ");sb.append(location.getLocType());sb.append("\n緯度坐標 : ");sb.append(location.getLatitude());sb.append("\n經(jīng)度坐標 : ");sb.append(location.getLongitude());sb.append("\n定位精度 : ");sb.append(location.getRadius());if (location.getLocType() == BDLocation.TypeGpsLocation) {// 如果是GPS定位結(jié)果sb.append("\n獲取速度(僅gps定位) : ");sb.append(location.getSpeed());sb.append("\n獲取gps鎖定用的衛(wèi)星數(shù) : ");sb.append(location.getSatelliteNumber());sb.append("\n 獲取手機當前的方向 : ");sb.append(location.getDirection());} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 如果是網(wǎng)絡定位結(jié)果sb.append("\n獲取詳細地址信息: ");sb.append(location.getAddrStr());sb.append("\n獲取運營商信息 : ");sb.append(location.getOperators());}GeoPoint geoPoint = new GeoPoint((int) (location.getLatitude() * 1E6),(int) (location.getLongitude() * 1E6));// 將給定的位置點以動畫形式移動至地圖中心 mMapView.getController().animateTo(geoPoint);logMsg(sb.toString());}// 接收異步返回的POI查詢結(jié)果 @Overridepublic void onReceivePoi(BDLocation arg0) {// TODO Auto-generated method stub }private void logMsg(String string) {// TODO Auto-generated method stubLog.i("MyLocationListener", string);}}?
3、設置定位參數(shù)
?
LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);//設置定位模式option.setCoorType("bd09ll");//返回的定位結(jié)果是百度經(jīng)緯度,默認值gcj02option.setScanSpan(5000);//設置發(fā)起定位請求的間隔時間為5000msoption.setIsNeedAddress(true);//返回的定位結(jié)果包含地址信息option.setNeedDeviceDirect(true);//返回的定位結(jié)果包含手機機頭的方向 mLocationClient.setLocOption(option);// 裝在定位的屬性mLocationClient.setLocOption(option);?
4、開始定位
// 啟動定位sdk mLocationClient.start();// 設置定位數(shù)據(jù)if (mLocationClient != null && mLocationClient.isStarted())// 請求定位,異步返回,結(jié)果在locationListener中獲取. mLocationClient.requestLocation();elseLog.d(tag, "locClient is null or not started");}?
Done
轉(zhuǎn)載于:https://www.cnblogs.com/xingyyy/p/3538312.html
總結(jié)
以上是生活随笔為你收集整理的百度地图API使用之实现定位的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery 使用 jQuery UI
- 下一篇: Core Text 入门