package com.tarena.allrun.activity;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.platform.comapi.map.h;
import com.tarena.allrun.R;
import com.tarena.allrun.TApplication;
import com.tarena.allrun.util.BaiduMapUtil;
import com.tarena.allrun.util.ExceptionUtil;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SportFragment extends Fragment {
View view;
// 管理地图
BaiduMap baiduMap;
// 定位客户端
LocationClient locationClient;
MapView mapView;
Handler handler = new Handler();
AlertDialog counterDialog;
TextView tvAction;
int count = 3;
//移动的点的集合
ArrayList<LatLng> positionList = new ArrayList<LatLng>();
LinearLayout linearLayout;
Runnable runnable;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try {
Log.i("Fragment次数", "onCreateView");
view = View.inflate(getActivity(), R.layout.fragment_sport, null);
setView();
addListener();
// 2.4 让框架中的接口指向实现类
locationClient = new LocationClient(this.getActivity());
// 实现类
MyBdlocationListener myBdlocationListener = new MyBdlocationListener();
locationClient.registerLocationListener(myBdlocationListener);
// 每隔2秒得一次坐标
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
// 少于1000,只得一次坐标,
option.setScanSpan(1);
// 单击定位按钮 locationClient.requestLocation();
option.setCoorType("bd09ll");
locationClient.setLocOption(option);
// 开始
locationClient.start();
} catch (Exception e) {
ExceptionUtil.handleException(e);
}
return view;
}
private void setView() {
// TODO Auto-generated method stub
// 初始化控件
mapView = (MapView) view.findViewById(R.id.mapView);
baiduMap = mapView.getMap();
tvAction = (TextView) view.findViewById(R.id.tv_fragment_sport_action);
//用来显示 移动时间 移动距离的布局
linearLayout = (LinearLayout) view.findViewById(R.id.ll_sport_recorder);
}
//添加监听
private void addListener() {
//给开始移动按钮添加监听
tvAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
TextView tvAction = (TextView) v;
String text = tvAction.getText().toString();
// 判断是开始还是停止
if ("开始".equals(text)) {
// 显示 3,2,1
counterDialog = new AlertDialog.Builder(getActivity())
.create();
View showCounterView = View.inflate(getActivity(),
R.layout.activity_show_counter, null);
final TextView tvCounter = (TextView) showCounterView
.findViewById(R.id.tv_show_count);
counterDialog.setView(showCounterView);
counterDialog.show();
handler.postDelayed(new Runnable() {
@Override
public void run() {
try {
count = count - 1;
tvCounter.setText(String.valueOf(count));
if (count > 0) {
handler.postDelayed(this, 500);
} else {
counterDialog.dismiss();
count = 3;
showRecorder();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, 500);
tvAction.setText("结束");
} else if ("结束".equals(text)) {
linearLayout.setVisibility(View.GONE);
handler.removeCallbacks(runnable);
positionList.clear();
baiduMap.clear();
tvAction.setText("开始");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
//给地图添加监听
baiduMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public boolean onMapPoiClick(MapPoi arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onMapClick(LatLng clickPosition) {
try {
Log.i("单击坐标点", "纬度" + clickPosition.latitude + ",经度="
+ clickPosition.longitude);
positionList.add(clickPosition);
if (positionList.size() >= 2) {
baiduMap.clear();
// 画线
PolylineOptions options = new PolylineOptions();
// 线用到坐标点
options.points(positionList);
baiduMap.addOverlay(options);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
// 2.3 写实现类
class MyBdlocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
try {
// 4.9E-324:模拟器上运行,真机上没信号
// 纬度
double latitude = bdLocation.getLatitude();
// 经度
double longitude = bdLocation.getLongitude();
Log.i("接收坐标", "纬度=" + latitude + ",经度=" + longitude);
// 在模拟器上运行,假定一个位置
if (latitude == 4.9E-324) {
latitude = 39.881726;
longitude = 116.472307;
}
// 移动地图中心点
// 坐标实体类
LatLng currentPosition = new LatLng(latitude, longitude);
// 移动地图,改变地图状态
// 17:地图显示的级别 4-17
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory
.newLatLngZoom(currentPosition, 17);
// animate 以动画方式移动
baiduMap.animateMapStatus(mapStatusUpdate);
// 在当前位置显示一个标志
MarkerOptions markerOptions = new MarkerOptions();
// 标志的位置
markerOptions.position(currentPosition);
// 标志的图片
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
.fromResource(R.drawable.icon_marka);
markerOptions.icon(bitmapDescriptor);
// 把标志添加到地图上
baiduMap.addOverlay(markerOptions);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//这个是显示移动时间 移动距离的 最后转换成速度
private void showRecorder() {
linearLayout.setVisibility(View.VISIBLE);
// 显示时间
// 显示时间控件
final Chronometer chronometer = (Chronometer) view
.findViewById(R.id.chronometer1);
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
final TextView tvDistance = (TextView) view.findViewById(R.id.tv_distance);
tvDistance.setText("0:0");
final TextView tvSpeed = (TextView) view.findViewById(R.id.tv_recorder_speed);
tvSpeed.setText("0:0");
runnable = new Runnable() {
@Override
public void run() {
try {
// 计算公里
double distance = 0;
if (positionList.size() >= 2) {
for (int i = 0; i < positionList.size() - 1; i++) {
// 第一个点 纬度
double latitude1 = positionList.get(i).latitude;
// 第一个点的经度
double longitude1 = positionList.get(i).longitude;
// 第二个点的纬度
double latitude2 = positionList.get(i + 1).latitude;
// 第二个点的经度
double longitude2 = positionList.get(i + 1).longitude;
distance = distance
+ BaiduMapUtil.GetDistance(longitude1,
latitude1, longitude2, latitude2);
}
// 转成公里
String strDistance = String.valueOf(distance / 1000);
if (strDistance.contains(".")) {
strDistance = strDistance.substring(0,
strDistance.indexOf(".") + 3);
}
tvDistance.setText(strDistance);
}
// 得时间60:00 [60,00]
String time = chronometer.getText().toString();
Log.i("计算时间", time + "," + System.currentTimeMillis());
double minute = Integer.parseInt(time.split("\\:")[0]);
double second = Integer.parseInt(time.split("\\:")[1]);
if ((minute > 0 || second > 0) && distance > 0) {
// 计算速度
double speed = (distance / 1000)
/ ((minute * 60 + second) / (60 * 60));
String strSpeed = String.valueOf(speed);
if (strSpeed.contains(".")) {
strSpeed = strSpeed.substring(0,
strSpeed.indexOf(".") + 3);
}
tvSpeed.setText(strSpeed);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
handler.postDelayed(this, 1000);
}
}
};
handler.postDelayed(runnable, 1000);
}
}
二 fragment对应的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eee" android:orientation="vertical" >
<RelativeLayout android:id="@+id/title" android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/title_bg_green" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="轨迹" android:textColor="#ffffff" android:textSize="18sp" />
</RelativeLayout>
<include layout="@layout/fragment_sport_recorder" android:visibility="gone"/>
<RelativeLayout android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/title_bg_green" android:gravity="center_vertical" >
<com.baidu.mapapi.map.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" android:clickable="true" />
<RelativeLayout android:id="@+id/relativeLayout_startSport" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="4dp" android:background="@drawable/btn_start_sport_selector" >
<TextView android:id="@+id/tv_fragment_sport_action" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="开始" android:textColor="@android:color/white" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>