onLocationChanged在每个Interval

时间:2016-10-31 09:38:46

标签: android geolocation

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

Please help me out here as i am stuck on it since last 3 days...

以下是我的代码,即设置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 个答案:

答案 0 :(得分:1)

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

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

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

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

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