public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Button.setOnClickListener(new ButtonListener());
// 判断GPS是否正常启动
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
// 返回开启GPS导航设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
return;
}
// 为获取地理位置信息时设置查询条件
String bestProvider = lm.getBestProvider(getCriteria(), true);
// 获取位置信息
// 如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
Location location = lm.getLastKnownLocation(bestProvider);
if (location != null) {
// tGpsDot tGpsDot1 = new tGpsDot();
tGpsDot1.latitude = location.getLatitude();
tGpsDot1.longitude = location.getLongitude();
tGpsDot1.point = location.getBearing();
}
// tGpsDot1.tGpsDot(location.getLongitude(), location.getLatitude(),
// location.getBearing());
// dot[0]=tGpsDot1;
updateView(location, 0);
// 监听状态
lm.addGpsStatusListener(listener);
// 绑定监听,有4个参数
// 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
// 参数2,位置信息更新周期,单位毫秒
// 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
// 参数4,监听
// 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
// 1秒更新一次,或最小位移变化超过1米更新一次;
// 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5,
locationListener);
dot[i]=tGpsDot1;
// if(tGpsDot1!=null)
// dot[i].tGpsDot(tGpsDot1.latitude,tGpsDot1.longitude,tGpsDot1.point);
// dot[i] = tGpsDot1;
// dot[i].tGpsDot(tGpsDot1.latitude,tGpsDot1.longitude,tGpsDot1.point);
// String x = String.valueOf(dot[0].latitude);
// Toast.makeText(MainActivity.this, x, Toast.LENGTH_LONG).show();
}
// 位置监听
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
// tGpsDot tGpsDot=new tGpsDot();
public void onLocationChanged(Location location) {
// String x = String.valueOf(dot[0].latitude);
// Toast.makeText(MainActivity.this, x, Toast.LENGTH_LONG).show();
tGpsDot1.latitude = location.getLatitude();
tGpsDot1.longitude = location.getLongitude();
tGpsDot1.point =location.getBearing();
/* try {
String pathName = "/sdcard/test/";
String fileName = "file3.bin";
File path = new File(pathName);
File file = new File(pathName + fileName);
if (!path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file, true);
String s = String.valueOf(location.getLongitude())+"aa"
+ String.valueOf(location.getLatitude())
+ String.valueOf(location.getSpeed())+"ss";
byte[] buf = s.getBytes();
stream.write(buf);
stream.close();
} catch (Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
e.printStackTrace();
}*/
String x = String.valueOf(dot[0].latitude);
Toast.makeText(MainActivity.this, x, Toast.LENGTH_LONG).show();
// dot[i].tGpsDot(tGpsDot1.latitude,tGpsDot1.longitude,tGpsDot1.point);
dot[i]= tGpsDot1;
// dot[i].longitude=tGpsDot1.longitude ;
// dot[i].point=tGpsDot1.point;
i++;
// }
if (i == 1199) {
i = 0;
}
updateView(location, i);
Log.i(TAG, "时间:" + location.getTime());
Log.i(TAG, "经度:" + location.getLongitude());
Log.i(TAG, "纬度:" + location.getLatitude());
Log.i(TAG, "海拔:" + location.getAltitude());
/*if(i>2)
tGpsDot2.add(tGpsDot1);*/
}
我想把一段时间的位置信息存放在dot[i]里边,可是直接用tGpsDot1等于的话会位置会保存最新的怎么才能保存以前的位置信息呢
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Button.setOnClickListener(new ButtonListener());
// 判断GPS是否正常启动
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();
// 返回开启GPS导航设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 0);
return;
}
// 为获取地理位置信息时设置查询条件
String bestProvider = lm.getBestProvider(getCriteria(), true);
// 获取位置信息
// 如果不设置查询要求,getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
Location location = lm.getLastKnownLocation(bestProvider);
if (location != null) {
// tGpsDot tGpsDot1 = new tGpsDot();
tGpsDot1.latitude = location.getLatitude();
tGpsDot1.longitude = location.getLongitude();
tGpsDot1.point = location.getBearing();
}
// tGpsDot1.tGpsDot(location.getLongitude(), location.getLatitude(),
// location.getBearing());
// dot[0]=tGpsDot1;
updateView(location, 0);
// 监听状态
lm.addGpsStatusListener(listener);
// 绑定监听,有4个参数
// 参数1,设备:有GPS_PROVIDER和NETWORK_PROVIDER两种
// 参数2,位置信息更新周期,单位毫秒
// 参数3,位置变化最小距离:当位置距离变化超过此值时,将更新位置信息
// 参数4,监听
// 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新
// 1秒更新一次,或最小位移变化超过1米更新一次;
// 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5,
locationListener);
dot[i]=tGpsDot1;
// if(tGpsDot1!=null)
// dot[i].tGpsDot(tGpsDot1.latitude,tGpsDot1.longitude,tGpsDot1.point);
// dot[i] = tGpsDot1;
// dot[i].tGpsDot(tGpsDot1.latitude,tGpsDot1.longitude,tGpsDot1.point);
// String x = String.valueOf(dot[0].latitude);
// Toast.makeText(MainActivity.this, x, Toast.LENGTH_LONG).show();
}
// 位置监听
private LocationListener locationListener = new LocationListener() {
/**
* 位置信息变化时触发
*/
// tGpsDot tGpsDot=new tGpsDot();
public void onLocationChanged(Location location) {
// String x = String.valueOf(dot[0].latitude);
// Toast.makeText(MainActivity.this, x, Toast.LENGTH_LONG).show();
tGpsDot1.latitude = location.getLatitude();
tGpsDot1.longitude = location.getLongitude();
tGpsDot1.point =location.getBearing();
/* try {
String pathName = "/sdcard/test/";
String fileName = "file3.bin";
File path = new File(pathName);
File file = new File(pathName + fileName);
if (!path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if (!file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file, true);
String s = String.valueOf(location.getLongitude())+"aa"
+ String.valueOf(location.getLatitude())
+ String.valueOf(location.getSpeed())+"ss";
byte[] buf = s.getBytes();
stream.write(buf);
stream.close();
} catch (Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
e.printStackTrace();
}*/
String x = String.valueOf(dot[0].latitude);
Toast.makeText(MainActivity.this, x, Toast.LENGTH_LONG).show();
// dot[i].tGpsDot(tGpsDot1.latitude,tGpsDot1.longitude,tGpsDot1.point);
dot[i]= tGpsDot1;
// dot[i].longitude=tGpsDot1.longitude ;
// dot[i].point=tGpsDot1.point;
i++;
// }
if (i == 1199) {
i = 0;
}
updateView(location, i);
Log.i(TAG, "时间:" + location.getTime());
Log.i(TAG, "经度:" + location.getLongitude());
Log.i(TAG, "纬度:" + location.getLatitude());
Log.i(TAG, "海拔:" + location.getAltitude());
/*if(i>2)
tGpsDot2.add(tGpsDot1);*/
}
我想把一段时间的位置信息存放在dot[i]里边,可是直接用tGpsDot1等于的话会位置会保存最新的怎么才能保存以前的位置信息呢