直接上我的代码然后注释给的很清楚
public class SportActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private MapView mapView;
private AMap aMap;
private TextView offlineMap, record;
private AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationOption;
private LocationSource.OnLocationChangedListener mListener;
private AMapLocation privLocation;
private SportMyView sportMyView;
private double distance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sport_avtivity);
//初始化DBFlow
FlowManager.init(new FlowConfig.Builder(this).build());
FlowManager.init(this);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);// 调用地图所必须重写
if (aMap == null) {
aMap = mapView.getMap();
}
aMap.getUiSettings().setZoomControlsEnabled(false);
aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
aMap.setMapTextZIndex(2);
setUpMap();
init();
insertModel();
}
private void setUpMap() {
/**
* 设置一些amap的属性
*/
UiSettings uiSettings = aMap.getUiSettings();
uiSettings.setCompassEnabled(true);// 设置指南针是否显示
uiSettings.setZoomControlsEnabled(true);// 设置缩放按钮是否显示
uiSettings.setScaleControlsEnabled(true);// 设置比例尺是否显示
uiSettings.setRotateGesturesEnabled(true);// 设置地图旋转是否可用
uiSettings.setTiltGesturesEnabled(true);// 设置地图倾斜是否可用
uiSettings.setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
/** 自定义系统定位小蓝点
*
*/
MyLocationStyle myLocationStyle = new MyLocationStyle();
aMap.setMyLocationStyle(myLocationStyle);
aMap.setLocationSource(mLocationSource);// 设置定位监听
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
}
/**
* 进入历史详情和地图管理页面
*/
private void init() {
offlineMap = findViewById(R.id.offlineMap);
offlineMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SportActivity.this, OfflineMapActivity.class);
startActivity(intent);
}
});
//历史记录
record = findViewById(R.id.record);
record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SportActivity.this, RecordActivity.class);
startActivity(intent);
}
});
}
public LocationSource mLocationSource = new LocationSource() {
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
sportMyView = findViewById(R.id.sportMyView);
sportMyView.setVisibility(View.INVISIBLE);
sportMyView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//初始化定位
initAmapLocation();
}
});
sportMyView.setVisibility(View.VISIBLE);
}
@Override
public void deactivate() {
mListener = null;
if (mLocationClient != null) {
mLocationClient.stopLocation();
mLocationClient.onDestroy();
}
mLocationClient = null;
}
};
/**
* 初始化定位
*/
private void initAmapLocation() {
//初始化定位
mLocationClient = new AMapLocationClient(this);
//设置定位回调监听
mLocationClient.setLocationListener(mAMapLocationListener);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
// 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)
mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.Transport);
//设置定位模式为AMapLocationMode.Hight_Accuracy,设备定位模式。
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
mLocationOption.setInterval(2000);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
mLocationOption.setHttpTimeOut(20000);
if (null != mLocationClient) {
mLocationClient.setLocationOption(mLocationOption);
//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
mLocationClient.startLocation();
}
}
/**
* 定位回调每1秒调用一次
*/
public AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
if (amapLocation.getErrorCode() == 0) {
mListener.onLocationChanged(amapLocation);// 显示系统小蓝点,不写这一句无法显示到当前位置
amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
Log.e(TAG, "获取经纬度集合" + privLocation);//打Log记录点是否正确
amapLocation.getAccuracy();//获取精度信息
amapLocation.getBearing();//获取方向角信息
amapLocation.getSpeed();//获取速度信息 单位:米/秒
amapLocation.getLocationType();//查看是什么类型的点
Log.e(TAG, "获取点的类型" + amapLocation.getLocationType());
if (amapLocation.getLocationType() == 1) {
Location location = new Location();
location.latitude = amapLocation.getLatitude();
location.longitutd = amapLocation.getLongitude();
drawLines(amapLocation);//一边定位一边连线
distance += distance;
Toast.makeText(SportActivity.this, "经纬度"+distance+"KM",Toast.LENGTH_SHORT).show();
Log.e("DDDDDDDDD", String.valueOf(distance));
Log.e(TAG, "获取点的类型" + amapLocation.getLocationType());
Log.e("LLLLL", String.valueOf(location.latitude));
Log.e("LLLLLLLL", String.valueOf(location.longitutd));
}
//获取定位时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(amapLocation.getTime());
sdf.format(date);
privLocation = amapLocation;
} else {
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo());
}
}
}
};
/**
* 绘制运动路线
*
* @param curLocation
*/
public void drawLines(AMapLocation curLocation) {
if (null == privLocation) {
return;
}
PolylineOptions options = new PolylineOptions();
//上一个点的经纬度
options.add(new LatLng(privLocation.getLatitude(), privLocation.getLongitude()));
//当前的经纬度
options.add(new LatLng(curLocation.getLatitude(), curLocation.getLongitude()));
options.width(10).geodesic(true).color(Color.GREEN);
aMap.addPolyline(options);
//距离的计算
distance = AMapUtils.calculateLineDistance(new LatLng(privLocation.getLatitude(),
privLocation.getLongitude()), new LatLng(curLocation.getLatitude(),
curLocation.getLongitude()));
}
/**
* 向数据库中传递数据
*/
private void insertModel() {
MyDataBase myDatabase = new MyDataBase();
myDatabase.averagespeed = "100";
myDatabase.duration = "23";
myDatabase.distance = distance;
myDatabase.time = "2018-10-23";
//myDatabase.save();
Log.e("TEST", String.valueOf(myDatabase.averagespeed));
Log.e("Sunday", String.valueOf(myDatabase.id));
Log.e("Location", String.valueOf(myDatabase.location));
}
/**
* 必须重写的方法
*/
@Override
protected void onDestroy() {
mapView.onDestroy();
super.onDestroy();
}
/**
* 必须重写的方法
*/
@Override
protected void onPause() {
mapView.onPause();
super.onPause();
}
/**
* 必须重写的方法
*/
@Override
protected void onResume() {
mapView.onResume();
super.onResume();
}
}
上面的代码中直接复制就行,泛红的方法删除就可以是用了。
其实高德地图定位有三种模式:基站定位、Wi-Fi定位、GPS定位。其中基站,精度在500米-5000米之间、Wi-Fi定位精度较高,在5米-200米之间、GPS精度较高,在10米-100米左右。所以记录运动轨迹是一般会把基站定位和Wi-Fi定位的点过滤掉,这样就很少飘点。
写的不好请指教。
文章地址:https://github.com/xindiyang/coddonGMap.git