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