1. 程式人生 > >百度地圖:定位,軌跡,白板,載入崩潰,定位到非洲

百度地圖:定位,軌跡,白板,載入崩潰,定位到非洲

這兩天道長在弄百度地圖,開發中也遇到了一些問題,在這裡和大家分享一下(額,主要是記錄一下,省的下次又忘了)

一、準備

二、定位

1.百度地圖配置

  • 新增許可權
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"
/>
<uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.GET_TASKS"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  • 新增百度金鑰
        <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="百度金鑰" />
  • 新增百度定位服務
        <!-- 百度定位SDK  service  BEGIN -->
        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.baidu.location.service_v2.2" >
                </action>
            </intent-filter>
        </service>

2.百度地圖定位實現

  • 初始化百度地圖
    private void initMap() {
//      獲取地圖控制元件引用
        mMapView.removeViewAt(2);
//      不顯示縮放比例尺
        mMapView.showZoomControls(false);
//      百度地圖
        mBaiduMap = mMapView.getMap();
    }
  • 初始化定位
    private void initLocation() {
//      定位客戶端的設定
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationListener = new MyLocationListener();
//      註冊監聽
        mLocationClient.registerLocationListener(mLocationListener);
//      配置定位
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);// 開啟gps
        option.setCoorType("bd09ll"); // 設定座標型別
        option.setScanSpan(3000); // 呼叫監聽時間間隔
        mLocationClient.setLocOption(option);

    }
  • 實現定位監聽介面

    /**
     * 定位SDK監聽介面
     */
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 銷燬後不在處理新接收的位置
            if (location == null || mMapView == null)
                return;
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();

            mBaiduMap.setMyLocationData(locData);

            if (isFirst == true){
                LatLng cenpt = new LatLng(location.getLatitude(), location.getLongitude());
//                LatLng cenpt = new LatLng(40.051558, 116.314941);
                MapStatus mMapStatus = new MapStatus.Builder().target(cenpt).zoom(14).build();
                MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
                mBaiduMap.animateMapStatus(mMapStatusUpdate);
                isFirst = false;
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }

    }
  • 定位到某一個點
    這個是根據經緯度定位並移動到一個點,然後在上面新增圖層
    private void refreshActivityView() {
//      改變地圖狀態  40.051558,116.314941  29.806651,121.606983
//        LatLng cenpt = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
        LatLng cenpt = new LatLng(40.051558, 116.314941);
        MapStatus mMapStatus = new MapStatus.Builder().target(cenpt).zoom(14).build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        mBaiduMap.animateMapStatus(mMapStatusUpdate);

        OverlayOptions ooA = new MarkerOptions().position(cenpt).icon(BitmapDescriptorFactory
                .fromResource(R.drawable.club_track_location))
                .zIndex(10).draggable(true);
        mBaiduMap.addOverlay(ooA);
    }

三、軌跡

軌跡就是在百度地圖上新增一個圖層,把定位資訊繪製在圖層上

  • 繪製軌跡圖層
private void drawTrackLine(BDLocation location) {

        dot = new LatLng(location.getLatitude(), location.getLongitude());
        if (startTrack == true) {
            line.add(dot);
            if (line.size() == 1) {
                // 這裡繪製起點
                drawStart(line);
                if (isPause != true) {
                    ToastUtil.showCenter(ClubTrackActivity.this, "軌跡已開啟");
                }

            } else if (line.size() > 3) {
                points_tem = line.subList(line.size() - 2, line.size());

                options = new PolylineOptions().color(0xfe12b7f5).width(10)
                        .points(points_tem);
                mBaiduMap.addOverlay(options);
            }
        }
    }
  • 繪製起點、終點
 /**
     * 繪製起點,取前n個點座標的平均值繪製起點
     *
     * @param points2
     */
    public void drawStart(List<LatLng> points2) {
        double myLat = 0.0;
        double myLng = 0.0;

        for (LatLng ll : points2) {
            myLat += ll.latitude;
            myLng += ll.longitude;
        }
        LatLng avePoint = new LatLng(myLat / points2.size(), myLng
                / points2.size());
        line.add(avePoint);
        OverlayOptions ooA;
        if (setDot == false) {
            ooA = new MarkerOptions().position(avePoint).icon(BitmapDescriptorFactory
                    .fromResource(R.drawable.club_track_start))
                    .zIndex(4).draggable(true);
        } else {
            ooA = new DotOptions().center(avePoint).color(0xfe12b7f5).radius(15);
        }
        mBaiduMap.addOverlay(ooA);
    }

    /**
     * 繪製終點。
     *
     * @param points2
     */
    protected void drawEnd(List<LatLng> points2) {
        double myLat = 0.0;
        double myLng = 0.0;
        if (points2.size() >= 2) {
            for (int i = points2.size() - 2; i < points2.size(); i++) {
                LatLng ll = points2.get(i);
                myLat += ll.latitude;
                myLng += ll.longitude;

            }
            LatLng avePoint = new LatLng(myLat / 2, myLng / 2);

            OverlayOptions ooA;
            if (setDot == false) {
                ooA = new MarkerOptions().position(dot).icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.club_track_close))
                        .zIndex(4).draggable(true);
            } else {
                ooA = new DotOptions().center(avePoint).color(0xfe12b7f5).radius(15);
                mBaiduMap.addOverlay(ooA);
            }
            setDotSuc = true;
            mBaiduMap.addOverlay(ooA);
        } else {

            if (isPause == false && setDot == false) {
                OverlayOptions ooA = new MarkerOptions().position(dot).icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.club_track_close))
                        .zIndex(4).draggable(true);

                setDotSuc = true;
                mBaiduMap.addOverlay(ooA);

            }
        }
    }

四、遇到的bug

1.白板

描述:載入地圖時顯示白板,沒有網格
原因:經緯度傳反了

2.載入崩潰

描述:百度地圖在華為mate8上載入崩潰,異常內容:

java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.wanbu.dascom-1/lib/arm64/libBaiduMapSDK_v3_2_0_15.so" is 32-bit instead of 64-bit

原因:Android5.0以後支援64位的手機晶片,但是百度地圖在3.5.0開始對64位的手機晶片開始支援,在Android5.0+的64位手機上使用3.5.0之前版本的百度地圖SDK會崩潰。
解決方法:更新百度地圖SDK

3.定位到非洲

描述:百度地圖定位到非洲幾內亞灣。當時道長看到這個現象是崩潰的,這是什麼鬼~~~
使用百度地圖的Api查詢原因:

            int code = location.getLocType();
            ToastUtil.showCenter(getApplicationContext(), "key= " + code);

道長返回的code值為505,可以檢視原始碼:

public void setLocType(int var1) {
        this.mLocType = var1;
        switch(var1) {
        case 61:
            this.setLocTypeDescription("GPS location successful!");
            this.setUserIndoorState(0);
            break;
        case 62:
            this.setLocTypeDescription("Location failed beacuse we can not get any loc information!");
            break;
        case 63:
        case 67:
            this.setLocTypeDescription("Offline location failed , please check the net (wifi/cell)!");
            break;
        case 66:
            this.setLocTypeDescription("Offline location successful!");
            break;
        case 161:
            this.setLocTypeDescription("NetWork location successful!");
            break;
        case 162:
            this.setLocTypeDescription("NetWork location failed because baidu location service can not decrypt the request query, please check the so file !");
            break;
        case 167:
            this.setLocTypeDescription("NetWork location failed because baidu location service can not caculate the location!");
            break;
        case 505:
            this.setLocTypeDescription("NetWork location failed because baidu location service check the key is unlegal, please check the key in AndroidManifest.xml !");
            break;
        default:
            this.setLocTypeDescription("UnKnown!");
        }

    }

可以看到道長的key是違法的,重新申請了一個key更換上就可以了

關於百度地圖的暫時到這裡,以後有新的內容再分享。希望這篇部落格能為你提供一些幫助
demo中的百度key由於某些原因去掉了,記得使用時要重新填寫!重新填寫!重新填寫!

原始碼下載