How to return the value given by the gps within a function

PinguinoTI

I am trying to return the result value that gives me the sum of latitude and longitude, but the function is giving me back the default value

Double result = 0.1;

public Double codeQR(final Context context, final Activity activity){

    client2 = LocationServices.getFusedLocationProviderClient(context);
    client2.getLastLocation().addOnSuccessListener(activity, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            latitude = location.getLatitude();
            longitude = location.getLongitude();
            result = (latitude +longitude);

        }

    });


   return result;
}

I expected the result of the sum of latitude and longitude, but it is returning 0.1

VeryDarkCode

Like Andy said, the OnSuccessListener is asynchronous, so it will runs on a different Thread while your return value is on the main Thread.

You have different workaround :

Workaround 1) Declare in your scope a global result variable, that you can reuse anywhere in your class after you set it with your OnSuccessListener :

private Double result;

Workaround 2) Pass it to a method directly in its parameter :

public void codeQR(final Context context, final Activity activity){

    client2 = LocationServices.getFusedLocationProviderClient(context);
    client2.getLastLocation().addOnSuccessListener(activity, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            latitude = location.getLatitude();
            longitude = location.getLongitude();
            result = (latitude +longitude);

            anotherMethod(result);
        }
    });
}

Workaround 3) Create a callback Interface, put this callback in your method parameter, and then call your method wherever you need to access the "result" value :

 //A) Create a callback Interface
 public interface MyCallback {
    void onCallback(Double result);
}

//B) Put this callback in your method parameter
public void codeQR(final Context context, final Activity activity, MyCallback myCallback){

    client2 = LocationServices.getFusedLocationProviderClient(context);
    client2.getLastLocation().addOnSuccessListener(activity, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {

            latitude = location.getLatitude();
            longitude = location.getLongitude();
            result = (latitude +longitude);

            myCallback.onCallback(result);
            }
        });
    }

    //C) Call your method wherever you need to access the "result" value
    //For example, if you want to get the result value in the nextStep() method
    private void nextStep(){
       codeQR(this, this, new MyCallback() {
          @Override
          public void onCallback(Double result) {
                    Log.i("TestOfResult", "onCallback: " + result);
                }
          });
     }

You can find more explanations about this interface on this link. I hope this will help you, let us know if this worked or if you have questions !

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to return a value of a function given as a command in tkinter

How to return a value within a function with an event listener?

How to return the value of function 2 from within function 1

How to get value return from a function within a loop?

How can I return a value within a nested function?

Swift: How do I return a value within an asynchronous urlsession function?

How do I pass a return value to another function and assign the return value to a variable within that function?

How to return a function within a function and display it in return

How to return an object with a given value?

Get the return value of a single function within Promise

Sequelize does not return a value within a function

How can I find the specific interval (via list) within which a given value occurs, in order to return the corresponding value from a second list?

How to return a value from a function, which is generated inside a callback function within?

How to call stored procedure within function by output value, then get and return this value?

(Swift) Confused between a return value of a for-in loop within a function, and a return value that comes after the loop (within the function))

How to return the value of given key in method - dictionary

How to check date within two given dates and return true or false

How to return the null values and value within the same column by using textbox search function

How to create functions within a function and return them?

How to return an input within a def function with python

How to return a value within a promise in angular 2

Recursive function how to preservce the value within function

How to destructure the return value of a function?

How to return value from this function?

How to return a value from a function

how to return function value in javascript

How to return the value of a swap function

How return a value with the input function?

How to return a callback's function return value