AndroidBDMap学习05:连续定位地图

时间:2021-03-13 17:58:49

一、防抖动,平滑策略

利用历史记录与当前定位信息,来确定图层标记的位置,以防止抖动

评分方案:

//权重分配
public static float[] EARTH_WEIGHT = {0.1f,0.2f,0.4f,0.6f,0.8f}; 

历史位置与当前定位结合:

//评分
for (int i = 0; i < locationList.size();   i) {//获得一个历史积累评分
    //历史lastPoint与当前curPoint的比对
    LatLng lastPoint = new LatLng(locationList.get(i).location.getLatitude(),
            locationList.get(i).location.getLongitude());
    LatLng curPoint = new LatLng(location.getLatitude(), location.getLongitude());
    double distance = DistanceUtil.getDistance(lastPoint, curPoint);//距离计算
    curSpeed = distance / (System.currentTimeMillis() - locationList.get(i).time) / 1000;
    score  = curSpeed * Utils.EARTH_WEIGHT[i];
}
if (score > 0.00000999 && score < 0.00005) { // 经验值,开发者可根据业务自行调整,也可以不使用这种算法
    //调整,取上一次与当前的平均位置
    location.setLongitude(
            (locationList.get(locationList.size() - 1).location.getLongitude()   location.getLongitude())
                    / 2);
    location.setLatitude(
            (locationList.get(locationList.size() - 1).location.getLatitude()   location.getLatitude())
                    / 2);
    locData.putInt("iscalculate", 1);
} else {
    locData.putInt("iscalculate", 0);
}

二、添加图层标签

//1.设定标签
bitmap = BitmapDescriptorFactory.fromResource(R.drawbla....)
//2.设置图层变化
OverlayOption option=new MarkerOption().position(point).icon(bitmap)
//3.添加并显示
mBaiduMap.addOverlay(option)
//4.更新状态
mBaiduMap.setMapStatus(MapStatusFactory.newLocation(point))

三、生命周期管理

onDestroy:
    unrigestorListener(...)
    locationService.stop()
    mMapView.onDestroy()
onResume:
    mMapView.onResume()
    reset Button deal with...
    mMapView.clear()
onPause:
    mMapView.onPause()