gpt4 book ai didi

android - onLocationChanged 在每个时间间隔调用两次

转载 作者:行者123 更新时间:2023-11-30 00:54:51 25 4
gpt4 key购买 nike

我正在编写一个基于位置的 Android 应用程序,我在其中应用位置监听器来捕获 GPS 坐标、速度和覆盖距离。问题是我的“onLocationChanged”回调函数在每个时间间隔被调用两次,导致将值相加 2 次而不是 1 次。这是我的 onLocationChanged 代码:

@Override
public void onLocationChanged(Location location) {

/* currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();

driverLoc.setDriver_latitude(currentLatitude);
driverLoc.setDriver_longitude(currentLongitude);

Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "" + (location.getSpeed() * 3.6f), Toast.LENGTH_LONG).show();
*/
latitude = location.getLatitude();
longitude = location.getLongitude();

driverLoc.setDriver_latitude(latitude);
driverLoc.setDriver_longitude(longitude);

long elapsedMillis = SystemClock.elapsedRealtime();
//Toast.makeText(JourneyStartActivity.this, "Elapsed milliseconds: " + (elapsedMillis/1000),
// Toast.LENGTH_SHORT).show();

long Milis = previousMiliSecs;

previousMiliSecs = elapsedMillis;

int hours = (int) (elapsedMillis / 3600000);
int minutes = (int) (elapsedMillis - hours * 3600000) / 60000;

long secs = TimeUnit.MILLISECONDS.toSeconds((previousMiliSecs - Milis));

long currentMilis = previousMiliSecs - Milis;

if( location.getAccuracy() <=10 && (previousMiliSecs - Milis) >= 10000) {

Location temp = mCurrentLocation;

mCurrentLocation = location;

float[] fArr;
fArr = new float[3];

double distance = mCurrentLocation.distanceTo(temp);

//location.distanceBetween(temp.getLatitude(), temp.getLongitude(), mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), fArr);

//double distance = getDistanceFromLatLonInKm(temp.getLatitude(), temp.getLongitude(), mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
//double distance = fArr[0];

float journeySpeed = (location.getSpeed() * 3.6f);
//float journeySpeed = (float) ((distance / 10.0f) * 3.6f);
//journeySpeed *= 3.6f;

if (GPSTracker.inJourney) {
journey.setTotalDistanceCovered(distance);
journey.setJourneySpeedIntervals(journeySpeed);
}
/*
TextView txt = (TextView) ((Activity) mContext).findViewById(R.id.textView38);
txt.setText(String.valueOf(journey.getTotalDistanceCovered()));
*/
//Log.e("Home GPS Update","Lat: " + latitude + " Long: " + longitude + " Speed: " + journeySpeed + " Distance: " + journey.getTotalDistanceCovered());
Log.e("Home GPS Update", "Lat: " + latitude + " Long: " + longitude + " Speed: " + journeySpeed + " Distance: " + journey.getTotalDistanceCovered() + " Accuracy: " + location.getAccuracy() + " Seconds: " + secs);
}
}

自从过去 3 天以来,我一直卡在这里,请帮帮我...

这是我设置 Google API 客户端的代码:

@Override
public void onConnected(Bundle bundle) {

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}

mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(0);
mLocationRequest.setInterval(1 * 10000); // 10 seconds, in milliseconds
mLocationRequest.setFastestInterval(1 * 10000); // 10 second, in milliseconds

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else {
//If everything went fine lets get latitude and longitude
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();

driverLoc.setDriverLocation(location);

driverLoc.setDriver_latitude(currentLatitude);
driverLoc.setDriver_longitude(currentLongitude);

//Toast.makeText(this, "Latitude: "+driverLoc.getDriver_latitude() + " Longitude: "+driverLoc.getDriver_longitude(), Toast.LENGTH_LONG).show();
}
}


10-31 15:38:07.854 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 14
10-31 15:38:07.855 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 14
10-31 15:38:17.856 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 10
10-31 15:38:17.858 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 10
10-31 15:38:32.853 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.9 Seconds: 14
10-31 15:38:32.854 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.9 Seconds: 14

最佳答案

检查您是否注册了两次相同的回调,也许这就是问题所在。您两次添加相同的监听器:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} else { ...

位置管理器没有缓存值,第一次返回 null。此时您再次注册同一个监听器。你应该忽略第一个空值

关于android - onLocationChanged 在每个时间间隔调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40339628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com