Java 在android中,每1分钟移动一次,如何获得不同的纬度和经度?
现在我正在使用GPS,以便在移动时每1分钟获得不同的纬度和经度,这必须存储到阵列列表中。我正在使用线程。我跟踪了这个链接 我的代码是 Tracking.javaJava 在android中,每1分钟移动一次,如何获得不同的纬度和经度?,java,android,gps,location,geopoints,Java,Android,Gps,Location,Geopoints,现在我正在使用GPS,以便在移动时每1分钟获得不同的纬度和经度,这必须存储到阵列列表中。我正在使用线程。我跟踪了这个链接 我的代码是 Tracking.java package com.example.getlatlang; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Butto
package com.example.getlatlang;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Tracking extends Activity
{
Button btnShowLocation;
boolean isRepeat = true;
protected int splashTime = 1000;
int timer =0;
Thread th;
ArrayList<Double> lat_array = new ArrayList<Double>();
ArrayList<Double> lon_array = new ArrayList<Double>();
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.currentlocation);
btnShowLocation = (Button) findViewById(R.id.start_bt);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// create class object
if(isRepeat)
{
isRepeat = false;
btnShowLocation.setBackgroundResource(R.drawable.stop);
if(lat_array.size()>0 && lon_array.size()>0)
{
lat_array.clear();
lon_array.clear();
System.out.println("Array cleared...");
}
// check if GPS enabled th=new Thread()
th=new Thread()
{
@Override
public void run(){
try
{
for (timer = 0; timer < 20; timer++)
{
// int waited = 0;
// while(waited < splashTime)
// {
Thread.sleep(100);
runOnUiThread(new Runnable()
{
@Override
public void run()
{
try
{ gps = new GPSTracker(Tracking.this);
if(gps.canGetLocation())
{
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
lat_array.add(latitude);
lon_array.add(longitude);
// \n is for new line
System.out.println("lat_array"+lat_array+"lon_array"+lon_array);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + lat_array + "\nLong: " + lon_array, Toast.LENGTH_LONG).show();
}
else
{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
// waited += 100;
//}
}}
catch (InterruptedException e)
{
}
}
};
th.start();
}
else
{
isRepeat = true;
th.interrupt();
btnShowLocation.setBackgroundResource(R.drawable.start);
}
}
});
}
public void ohDestroy()
{
th.stop();
}
}
现在我的问题是在移动时无法获得不同的纬度和经度点。第一个值仅存储到数组中20次。因为我正在设置踏板,必须像那样运行20次。但我想得到不同的点并存储到数组列表中。我不知道我在哪里犯的错误。有人能帮我解决这个问题吗?提前感谢。应用此功能。它将每10秒刷新一次
Handler mHandler1 = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(10000);
mHandler1.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// creating GPS Class object
GPSTracker gps = new GPSTracker (Tracking .this);
// check if GPS location have some values
if (gps.canGetLocation()) {
double currentlat = gps.getLatitude();
double currentlong = gps.getLongitude();
} else {
// no current location
gps.showSettingsAlert();
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
您在收集纬度的循环中使用了sleep(100)
。这里的时间单位是微秒。(据我所知)
20*100=2000,仅为2秒。我认为GPS的更新速度没有那么快。
看看这个。
相反,您可以使用相同的循环,但仅当值与前一个不同时才存储该值。
if(oldLat != curLat){
//store curLat to array
//and make oldLat = curLat
}
使用计划程序和服务我已更改,请查看。同意,我们可以要求应用程序每2秒刷新/更新一次(在应用程序级别),但不能保证每个刷新间隔,后续GPS“定位”或“热启动”(TTSF)可以每2秒发生一次(在设备级别);立即锁定GPS位置取决于已存储的卫星信息的可用性。没有多少GPS接收机能够实现如此快速的TTSF。。这一款声称它可以每2秒“热启动”(绝对不适用于智能手机设计)
if(oldLat != curLat){
//store curLat to array
//and make oldLat = curLat
}