0

guys. I have a problem, cause i am beginner in Android Dev. I need to get and to write user's GPS data to DB, but i don't know how to do it. If i trying to write data to variable, Android Studio says about error "Variable is accessed from within inner class, needs to be declared final".

import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class LogsHelper extends Service {
    public String action;
    public String item;
    FusedLocationProviderClient client;
    Context context;
    double lat;
    double lon;
    LatLng latLng;
    protected  Location location;
    Activity activity;
    public LogsHelper(String item, Context context, Activity activity) {
        this.item = item;
        this.context = context;
        this.activity = activity;
    }
    public void onCreate() {
        super.onCreate();
    }
    public void createLog(String old_item, String new_item, int action){
        this.getCoordinates();

    }
    public double[] getCoordinates(){
        double[] latLong = new double[2];
        requestPermission();
        client = LocationServices.getFusedLocationProviderClient(activity);
        client.getLastLocation().addOnCompleteListener(activity, new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                if(task.isSuccessful() && task.getResult() != null){
                    /*There is a problem*/
                    location = task.getResult();
                }
            }
        });
        return latLong;
    }
    private void requestPermission(){
        ActivityCompat.requestPermissions(activity, new String[]{ACCESS_FINE_LOCATION}, 1);
    }
}

1 Answers1

0

Location has two main attribute: longitude and latitude. combination of these 2 numbers represent a location.

if(location!=null) {

double latitude = location.getLatitude();

double longitude = location.getLongitude();   

}

Then you can store these 2 attribute on DB, further problems ?

Joe
  • 341
  • 2
  • 11