package android_Examples.ch14.ch14_5;
import android.app.Activity;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class Ch14_5Activity extends Activity {
private final int MAX_RECORDS = 10;
private LocationManager manager;
private Location currentLocation;
private String best;
private int index = 0, count = 0;
private double[] GPLat = new double[MAX_RECORDS];
private double[] GPLng = new double[MAX_RECORDS];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得系统服务的LocationManager对象
manager = (LocationManager)getSystemService(LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// 取得最佳的定位提供者
Criteria criteria = new Criteria();
best = manager.getBestProvider(criteria, true);
// 更新位置频率的条件
int minTime = 5000; // 毫秒
float minDistance = 15; // 公尺
if (best != null) { // 取得快取的最后位置,如果有的话
currentLocation = manager.getLastKnownLocation(best);
manager.requestLocationUpdates(best, minTime,
minDistance, listener);
}
else { // 取得快取的最后位置,如果有的话
currentLocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, listener);
}
updatePosition(); // 更新位置
}
@Override
protected void onPause() {
super.onPause();
manager.removeUpdates(listener);
}
// 更新现在的位置
private void updatePosition() {
TextView output, list;
String str = "最近个人行踪的坐标清单:\n";
output = (TextView) findViewById(R.id.output);
list = (TextView) findViewById(R.id.list);
if(currentLocation == null) {
output.setText("取得定位信息中...");
} else {
// 先下目前经纬度坐标信息
output.setText(getLocationInfo(currentLocation));
// 显示个人行踪的坐标清单
for (int i = 0; i < MAX_RECORDS; i++) {
if (GPLat[i] != 0.0)
str += GPLat[i] + "/" + GPLng[i] +"\n";
}
list.setText(str);
}
}
// 创建定位服务的监听者对象
private LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
updatePosition();
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
};
// 取得定位信息
public String getLocationInfo(Location location) {
boolean isSave = true;
double lat, lng;
lat = location.getLatitude();
lng = location.getLongitude();
StringBuffer str = new StringBuffer();
str.append("定位提供者(Provider): "+location.getProvider());
str.append("\n纬度(Latitude): " + Double.toString(lat));
str.append("\n经度(Longitude): " + Double.toString(lng));
if (count >= 1) { // 检查是否需要储存坐标
// 创建目地GPS坐标的Location对象
Location dest = new Location(location);
// 前一个坐标的数组索引
int preIndex = index - 1;
// 检查前一坐标是否是数组最后一个元素
if (preIndex < 0 ) preIndex = GPLat.length - 1;
// 设置目的Location对象的坐标
dest.setLatitude(GPLat[preIndex]);
dest.setLongitude(GPLng[preIndex]);
Log.d("Ch14_5", count + " index/preIndex: " + index +"/" +preIndex);
Log.d("Ch14_5", "dlat: " + GPLat[preIndex]);
Log.d("Ch14_5", "dlng: " + GPLng[preIndex]);
// 计算与目地坐标的距离
float distance = location.distanceTo(dest);
Toast.makeText(this, "距离: " + distance + "公尺",
Toast.LENGTH_SHORT).show();
Log.d("Ch14_5", "distance: " + distance);
// 检查距离是否小于 20公尺, 小于不用存
if (distance < 20.0) isSave = false;
}
if (isSave) { // 记录坐标
GPLat[index] = lat;
GPLng[index] = lng;
count++;
if (count >= MAX_RECORDS) count = MAX_RECORDS;
index++; // 数组索引加一
// 如果索引大于最大索引, 重设为0
if (index >= MAX_RECORDS) index = 0;
}
return str.toString();
}
// 启动Google地图
public void button1_Click(View view) {
// 取得经纬度坐标
float latitude = (float) currentLocation.getLatitude();
float longitude = (float) currentLocation.getLongitude();
// 创建 URI字符串
String uri = String.format("geo:%f,%f?z=18", latitude, longitude);
// 创建Intent对象
Intent geoMap = new Intent(Intent.ACTION_VIEW,Uri.parse(uri));
startActivity(geoMap); // 启动活动
}
// 在MapView组件显示个人行踪
public void button2_Click(View view) {
// 创建 Intent对象
Intent mapView = new Intent(this, MyMapActivity.class);
mapView.putExtra("GPSLATITUDE", GPLat);
mapView.putExtra("GPSLONGITUDE", GPLng);
mapView.putExtra("MAX_INDEX", count);
startActivity(mapView); // 启动活动
}
}