1. 程式人生 > >Android使用百度地圖SDK實現定位與方向感測器匹配

Android使用百度地圖SDK實現定位與方向感測器匹配

public class MylocationListener implements BDLocationListener
    {
        //定位請求回撥介面
        private boolean isFirstIn=true;
        //定位請求回撥函式,這裡面會得到定位資訊
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            //BDLocation 回撥的百度座標類,內部封裝瞭如經緯度、半徑等屬性資訊
            //MyLocationData 定位資料,定位資料建造器
            /*
            * 可以通過BDLocation配置如下引數
            * 1.accuracy 定位精度
            * 2.latitude 百度緯度座標
            * 3.longitude 百度經度座標
            * 4.satellitesNum GPS定位時衛星數目 getSatelliteNumber() gps定位結果時,獲取gps鎖定用的衛星數
            * 5.speed GPS定位時速度 getSpeed()獲取速度,僅gps定位結果時有速度資訊,單位公里/小時,預設值0.0f
            * 6.direction GPS定位時方向角度
            * */
            mLatitude= bdLocation.getLatitude();
            mLongitude=bdLocation.getLongitude();
            MyLocationData data= new MyLocationData.Builder()
                    .direction(mCurrentX)//設定圖示方向
                    .accuracy(bdLocation.getRadius())//getRadius 獲取定位精度,預設值0.0f
                    .latitude(mLatitude)//百度緯度座標
                    .longitude(mLongitude)//百度經度座標
                    .build();
            //設定定位資料, 只有先允許定點陣圖層後設置資料才會生效,參見 setMyLocationEnabled(boolean)
            mBaiduMap.setMyLocationData(data);
            //配置定點陣圖層顯示方式,三個引數的構造器
            /*
            * 1.定點陣圖層顯示模式
            * 2.是否允許顯示方向資訊
            * 3.使用者自定義定點陣圖標
            *
            * */
            MyLocationConfiguration configuration
                    =new MyLocationConfiguration(locationMode,true,mIconLocation);
            //設定定點陣圖層配置資訊,只有先允許定點陣圖層後設置定點陣圖層配置資訊才會生效,參見 setMyLocationEnabled(boolean)
            mBaiduMap.setMyLocationConfigeration(configuration);
            //判斷是否為第一次定位,是的話需要定位到使用者當前位置
            if(isFirstIn)
            {
                //地理座標基本資料結構
                LatLng latLng=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
                //描述地圖狀態將要發生的變化,通過當前經緯度來使地圖顯示到該位置
                MapStatusUpdate msu= MapStatusUpdateFactory.newLatLng(latLng);
                //改變地圖狀態
                mBaiduMap.setMapStatus(msu);
                isFirstIn=false;
                Toast.makeText(context, bdLocation.getAddrStr(), Toast.LENGTH_SHORT).show();
            }


        }
    }
4.方向感測器與定點陣圖標方向匹配操作: