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

Posted

技术标签:

【中文标题】onLocationChanged 在每个时间间隔调用两次【英文标题】:onLocationChanged Called Twice on every Interval 【发布时间】:2017-03-13 09:15:11 【问题描述】:

我正在编写一个基于位置的 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

【问题讨论】:

如果这被调用了两次,可能会显示这个之外的代码。你知道,导致这个被调用的那个 对不起,我没找到你...你指的是其他定位功能吗? 您正在显示被调用的代码,但没有显示如何将其设置为被调用。此代码不会调用自身。根本与问题无关。 我已添加代码 【参考方案1】:

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

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

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

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

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

【讨论】:

如何检查? 第一次注册监听器,如果响应为空,则第二次注册。 那么我应该删除哪一个? 你能给我正确的代码吗?这对我会有很大的帮助...... if (location != null) //如果一切顺利,让我们获取纬度和经度 currentLatitude = location.getLatitude(); currentLongitude = location.getLongitude(); driverLoc.setDriverLocation(位置); driverLoc.setDriver_latitude(currentLatitude); driverLoc.setDriver_longitude(currentLongitude);

以上是关于onLocationChanged 在每个时间间隔调用两次的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 设备上未调用 onLocationChanged()

在 Android 中未调用 onlocationchanged

需要同步 onLocationChanged

为啥在 API > 23 中不调用 FusedLocationApi (onLocationChanged)?

计算 onLocationChanged 方法之间的时间

OnLocationChanged 将不再工作,在许多设备上