百度定位繪製軌跡

1




百度定位回調監聽
此處注意;上次遇到個問題,在室內wifi能夠進入if語句,但室外不能夠定位,後來移動4g網絡下調試,
發現是getlocationtype沒有對應的,也就是沒有對移動網絡對locationType進行處理,
當時直接在百度給對demo那直接複製對也沒想那麼多,浪費了點時間。
	 
BDLocationListener listener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {//locationType “wf”代表wifi,“cl”代表基站,“ll”代表GPS,cl定位發起
	
            if (location != null && (location.getLocType() == 161 || location.getLocType() == 66 || location.getLocType() == 61)) {
                if (location.getRadius() > 50) {//此處能過濾部分定位模式切換導致的定位偏差
                    return;
                }
                if (location.getGpsAccuracyStatus() == BDLocation.GPS_ACCURACY_BAD) {
                    gpsStatus = "弱";
                } else if (location.getGpsAccuracyStatus() == BDLocation.GPS_ACCURACY_MID) {
                    gpsStatus = "中";
                } else if (location.getGpsAccuracyStatus() == BDLocation.GPS_ACCURACY_GOOD) {
                    gpsStatus = "強";
                }
		//此處爲自己業務去保存軌跡點位到本地數據庫
                saveTrack(algorithm(location));
            }
        }
    };

//百度提供定位處理方法
 private BDLocation algorithm(BDLocation location) {
        double curSpeed;
        if (locationList.isEmpty() || locationList.size() < 2) {
            TrackPointDto temp = new TrackPointDto();
            temp.setLatitude(location.getLatitude());
            temp.setLongitude(location.getLongitude());
            temp.setCreateTime(TimeTool.getDateString(System.currentTimeMillis()));
            locationList.add(temp);
        } else {//與前5次軌跡點進行比較
            if (locationList.size() > 5)
                locationList.removeFirst();
            double score = 0;
   
            for (int i = 0; i < locationList.size(); ++i) {
                LatLng lastPoint = new LatLng(locationList.get(i).getLatitude(), locationList.get(i).getLongitude());
                LatLng curPoint = new LatLng(location.getLatitude(), location.getLongitude());
                double distance = DistanceUtil.getDistance(lastPoint, curPoint);
                curSpeed = distance / (System.currentTimeMillis() - TimeTool.getLongTime(locationList.get(i).getCreateTime())) / 1000;
                score += curSpeed * Utils.EARTH_WEIGHT[i];
            }
//            if (score > 0.00000999 && score < 0.00005) { // 經驗值,開發者可根據業務自行調整,也可以不使用這種算法
            if (score > 0.00000999 && score < 0.00005) { // 經驗值,開發者可根據業務自行調整,也可以不使用這種算法
                location.setLongitude((locationList.get(locationList.size() - 1).getLongitude() + location.getLongitude()) / 2);
                location.setLatitude((locationList.get(locationList.size() - 1).getLatitude() + location.getLatitude()) / 2);
            }
            TrackPointDto newLocation = new TrackPointDto();
            newLocation.setLatitude(location.getLatitude());
            newLocation.setLongitude(location.getLongitude());
            newLocation.setCreateTime(TimeTool.getDateString(System.currentTimeMillis()));
            locationList.add(newLocation);
        }
        return location;
    }
	由於我的查看軌跡是在一個新開的activity上進行,所以要根據軌跡id查詢數據庫獲取點位進行繪製
	軌跡的id生成是  開始/暫停  一個時間段內進行重新生成,以達到繪製軌跡時能夠分段;
	下面是繪製軌跡的方法:
    /**
     * 畫軌跡
     */
    List<LatLng> latLngList = new ArrayList<>();
    MyLocationData locData;

    protected void drawMyRoute(List<LatLng> latLngList) {
        if (latLngList.size() > 1) {
            draw(latLngList);
        } else if (latLngList.size() > 0) {//如果只有一個點的話進行定位
            if (locData == null) {
                locData = new MyLocationData.Builder()
                        .accuracy(40.0f)
                        .direction(100).latitude(latLngList.get(0).latitude)
                        .longitude(latLngList.get(0).longitude).build();
                if (mBaiduMap != null) {
                    mBaiduMap.setMyLocationData(locData);
                    MapStatus.Builder builder = new MapStatus.Builder();
                    builder.target(latLngList.get(0)).zoom(zoom);
                    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
                }
            }
        }

    }

    private MapStatusUpdate msUpdate = null;
    private OverlayOptions overlay;  //覆蓋物
    private PolylineOptions polyline = null;  //路線覆蓋物
    private BitmapDescriptor realtimeBitmap;
    private Overlay overlay1;

    public void draw(List<LatLng> latLngList) {
        Log.i("drawTrack___", "draw");
        LatLng latLng = latLngList.get(latLngList.size() - 1);
        MapStatus mapStatus = new MapStatus.Builder().target(latLng).zoom(zoom).build();
        msUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
//        LatLngBounds bounds = new LatLngBounds.Builder().include(latLng).include(latLng).build();
//        msUpdate = MapStatusUpdateFactory.newLatLngBounds(bounds);
//        if (overlay != null) {
//            mBaiduMap.clear();
//        }
        if (overlay1 != null) {
            overlay1.remove();
        }
        realtimeBitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_gcoding);
        overlay = new MarkerOptions().position(latLng).icon(realtimeBitmap).zIndex(9).draggable(true);
        if (latLngList.size() >= 2) {// && pointList.size() <= 10000
            polyline = new PolylineOptions().width(15).color(Color.GREEN).points(latLngList);
        }
        //添加
        if (polyline != null && mBaiduMap != null) {
            overlay1 = mBaiduMap.addOverlay(polyline);
        }
        //添加圖層
        if (msUpdate != null && mBaiduMap != null) {
            mBaiduMap.setMapStatus(msUpdate);
        }
        //添加紅色小圖片
        if (overlay != null && mBaiduMap != null) {
            overlay1 = mBaiduMap.addOverlay(overlay);
        }

    }




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章