public class DrawPathActivity extends BaseActivity {
private MapView mMapView;
private BaiduMap mBaiduMap;
private static final int POINT_COUNT_PER_GROUP = 100;
private LatLng startPoint;
private Handler handler;
private HandlerThread thread;
private Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draw_path_activity);
mMapView = (MapView) this.findViewById(R.id.bmapView);
mBaiduMap = mMapView.getMap();
thread = new HandlerThread("drawPath");
thread.start();
handler = new Handler(thread.getLooper());
runnable = new Runnable() {
@Override
public void run() {
drawPath();
}
};
handler.post(runnable);
}
/**
* 绘制轨迹
*/
private void drawPath() {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
ArrayList<LatLng> pointsList = new ArrayList<LatLng>();
String latlngString = null;
try {
fileReader = new FileReader(SystemConstant.FILE_ROOT_DIR + File.separator + SystemConstant.POINT_FILE_NAME);
bufferedReader = new BufferedReader(fileReader);
while((latlngString = bufferedReader.readLine()) != null) {
String[] latlngArray = null;
latlngArray = latlngString.split(",");
double lat, lng;
lat = StringConversionUtil.parseDouble(latlngArray[0], 0);
lng = StringConversionUtil.parseDouble(latlngArray[1], 0);
LatLng point = new LatLng(lat, lng);
if(startPoint == null) {
startPoint = point;
}
//更新地图坐标
MapStatusUpdate u1 = MapStatusUpdateFactory.newLatLng(startPoint);
mBaiduMap.setMapStatus(u1);
//画点
OverlayOptions ooDot = new DotOptions().center(point).radius(8).color(0xFF0000FF);
mBaiduMap.addOverlay(ooDot);
pointsList.add(point);
//默认100个点为一组,画一次
if(pointsList.size() >= POINT_COUNT_PER_GROUP) {
//画线
OverlayOptions ooPolyline = new PolylineOptions().width(6).color(0xAAFF0000).points(pointsList);
mBaiduMap.addOverlay(ooPolyline);
pointsList.clear();
pointsList.add(point);
}
}
//如果循环结束后,pointsList的个数小于100,直接将剩下的所有点连线
OverlayOptions ooPolyline = new PolylineOptions().width(6).color(0xAAFF0000).points(pointsList);
mBaiduMap.addOverlay(ooPolyline);
pointsList.clear();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(bufferedReader != null) {
bufferedReader.close();
}
if(fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
mMapView.onResume();
super.onResume();
}
@Override
protected void onDestroy() {
if (handler != null) {
handler.removeCallbacks(runnable);
}
mMapView.onDestroy();
super.onDestroy();
}
}
我是在子线程里调用drawPath方法,这个方法主要就是从文件中读出经纬度然后在百度地图上画轨迹,每次执行的时候,如果轨迹没有完全画完就退出,程序就会崩溃,如果一直等待画完再退出,就不会崩溃
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。