0
對於速度測量應用程序,我需要每四秒鐘更新一次經度和緯度。我在兩次調用getLatitude和getLongitude函數之間使用了睡眠函數。但在屏幕上顯示的值仍然相同,即lon1和lon2,因爲雙打(十進制後的14位)具有完全相同的值,因此速度也顯示爲0.在固定的時間間隔後獲取緯度位置
如何在固定後獲取緯度和經度時間間隔(這裏4秒)? 非常感謝提前。
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
double r = 6371;
double lat1 = location.getLatitude();
double lon1 = location.getLongitude(); // To get initial longitude
SystemClock.sleep(4000); /////This seems ineffective.
double lat2 = location.getLatitude();
double lon2 = location.getLongitude(); // and here
double dlat = (lat2-lat1)*22/7/180;
double dlon = (lon2-lon1)*22/7/180;
textView.append("lon1:"+lon1+" lon2:"+lon2+" Kmph\n");
lat1 = lat1*22/7/180;
lat2 = lat2*22/7/180;
double h = ((1-Math.cos(dlat))/2) + Math.cos(lat1)*Math.cos(lat2)*(1-Math.cos(dlon))/2;
double d = 2**Math.asin(Math.sqrt(h));
double sp = d/3;
textView.append(" "+ sp + " Kmph\n");
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
},10);
return;
}
}else {
configureButton();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
configureButton();
return;
}
}
private void configureButton() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locationManager.requestLocationUpdates("gps", 4000, 0, locationListener);
}
});
}
}
你可以看看這只是改變30〜4 http://stackoverflow.com/questions/39314901/getting-latitude-and-longitude-in-30-seconds – Saveen
謝謝,但這個是沒有幫助的檢索當前和以前的緯度和經度值。 – Mani
它有助於檢索當前值,但爲什麼您需要以前的值? – Saveen