-3

I am trying to in-put a code I found in the following thread:

How do I get the current GPS location programmatically in Android?

I chose the second solution and input the code:

   private Location getLastBestLocation() {
        Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        long GPSLocationTime = 0;
        if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }

        long NetLocationTime = 0;

        if (null != locationNet) {
            NetLocationTime = locationNet.getTime();
        }

        if ( 0 < GPSLocationTime - NetLocationTime ) {
            return locationGPS;
        }
        else {
            return locationNet;
        }
    }
static final int TWO_MINUTES = 1000 * 60 * 2;


@override
public void onLocationChanged(Location location) {

    makeUseOfNewLocation(location);
    if(currentBestLocation == null){
        currentBestLocation = location;
    }

    ....
}


/**
 * This method modify the last know good location according to the arguments.
 *
 * @param location The possible new location.
 */
void makeUseOfNewLocation(Location location) {
    if ( isBetterLocation(location, currentBestLocation) ) {
        currentBestLocation = location;
    }
}

....

/** Determines whether one location reading is better than the current location fix
 * @param location  The new location that you want to evaluate
 * @param currentBestLocation  The current location fix, to which you want to compare the new one.
 */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location,
    // because the user has likely moved.
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must be worse.
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta;
    accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
        return provider2 == null;
    }
    return provider1.equals(provider2);
}
}

}

Because I lack the reputation level, I cannot ask on the thread directly, and thus this thread.

The Android Studio said that

'mLocationManager'

and

'currentBestLocation'

was not defined.

Can you figure out what's wrong with it? Thank you!

Also, for a side-note, I am trying to make this app work so that I can modify it to loop every 10 seconds and store variables to calculate speed

Community
  • 1
  • 1

2 Answers2

0

You need to define those fields, like the error mentions.

private Location currentBestLocation = null;

and

private mLocationManager = null;

// In onCreate, or something similar
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
TMH
  • 6,096
  • 7
  • 51
  • 88
  • I'm a real beginner, so I am not sure what the 'onCreate' command means. But thanks for your help! I'm now one step closer to finishing this task! – The Clueless Programmer Oct 08 '15 at 11:46
  • Your `Activity` will have an onCreate method, you can put it in there. If this helped you feel free to upvote/accept this answer. – TMH Oct 08 '15 at 11:56
  • 1
    Just an advice, if you are a beginner as you say, you should at least read https://developer.android.com/training/index.html and understand the basic notions before going any further. If you don't understand the lifecycle of an Android app you got get stuck on every single step you try to make. – Kalem Oct 08 '15 at 12:09
0

Android Studio generates the missing code. When you hold on the red marked problems, then a symbol is shown on the left side. By clicking on that symbol you can select one of the possiblities to generate the missing code you need.

Here there is a declaration of the 'mLocationManager'and 'currentBestLocation' missing. So Android Studio cannot resolve them. Create them before your onCreate() Method like:

private mLocationManager;
private Location currentBestLocation;

And set in onCreate():

mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

currentBestLocation = new Location;
Patricia
  • 2,885
  • 2
  • 26
  • 32