以下代码能够在地图上绘制自己走过的路径,有一个计时器,一个路程显示。按下“开始跑步”就可以开始绘制轨迹并同时开始计时。
以下是MainActivity文件
public class MainActivity extends Activity {
// int count=-0;
MapView mMapView = null;
LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();
private LocationMode mCurrentMode;
BitmapDescriptor mCurrentMarker;
// double distance=0;
BaiduMap mBaiduMap;
private Timer timer = new Timer();
private TimerTask task;
// List<LatLng> points = new ArrayList<LatLng>();
List<LatLng> pointstwo = new ArrayList<LatLng>();
LatLng p1;
LatLng p2;
// UI相关
OnCheckedChangeListener radioButtonListener;
Button requestLocButton, startButton,stopButton;
private TextView tvDistance;
boolean isFirstLoc = true;// 是否首次定位
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
// 调用相机回调接口由于MainActivity已经实现了回调接口,所以MainActivity.this即可
if (msg.what == 1) {
// if(points.size()>3){
// pointstwo.add((LatLng)points.get(points.size()-3));
// pointstwo.add((LatLng)points.get(points.size()-2));
// pointstwo.add((LatLng)points.get(points.size()-1));
// }else{
// pointstwo.add((LatLng)points.get(count));
// }
// Log.d("points",""+points.get(points.size()-3).latitude);
// Log.d("points",""+points.get(points.size()-2).latitude);
// Log.d("points",""+points.get(points.size()-1).latitude);
pointstwo.add(p1);
pointstwo.add(p2);
// distance+=DistanceUtil. getDistance(p1, p2);
// LatLng llDot = new LatLng(p2.latitude,p2.longitude);
// try{
OverlayOptions ooDot = new DotOptions().center(p2).radius(6)
.color(0xAAFF0000);
mBaiduMap.addOverlay(ooDot);
OverlayOptions ooPolyline = new PolylineOptions().width(4)
.color(0xAAFF0000).points(pointstwo);
mBaiduMap.addOverlay(ooPolyline);
p1 = p2;
mLocClient.requestLocation();
// }catch(Exception e){
// e.printStackTrace();
// }
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 使屏幕不显示标题栏(必须要在setContentView方法执行前执行)
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 隐藏状态栏,使内容全屏显示(必须要在setContentView方法执行前执行)
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 在使用SDK各组件之前初始化context信息,传入ApplicationContext
// 注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
/*
* forbid lock screen
*/
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
// 获取地图控件引用
Log.d("points", "wwww");
requestLocButton = (Button) findViewById(R.id.button1);
mCurrentMode = LocationMode.NORMAL;
requestLocButton.setText("普通");
OnClickListener btnClickListener = new OnClickListener() {
public void onClick(View v) {
switch (mCurrentMode) {
case NORMAL:
requestLocButton.setText("跟随");
mCurrentMode = LocationMode.FOLLOWING;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
case COMPASS:
requestLocButton.setText("普通");
mCurrentMode = LocationMode.NORMAL;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
case FOLLOWING:
requestLocButton.setText("罗盘");
mCurrentMode = LocationMode.COMPASS;
mBaiduMap
.setMyLocationConfigeration(new MyLocationConfiguration(
mCurrentMode, true, mCurrentMarker));
break;
}
}
};
requestLocButton.setOnClickListener(btnClickListener);
RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup);
radioButtonListener = new OnCheckedChangeListener() {