3

I am currently developing an app that requires your current location. I am using the Google Maps API and my problem is that when I run the app on a real phone, it gives the correct location, but when I am using the emulator it continues to give me a past location (that I have chosen a few days ago) and not the current location from the emulator.

I start by calling the getLocationPermission() in onCreateView() (I do these things in a fragment) and if this is granted it calls the getCurrentLocation() method.

I think the method works fine, but I don't get why cu location doesn't change when I change the location on the emulator.

Thank you in advance for taking some time to read this!

PS: my getCurrentLocation() method is this:

private void getCurrentLocation() {
        mFusedLocationClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                List<Address> addresses;

                if (task.isSuccessful()) {
                    mLastLocation = task.getResult();
                    if (mLastLocation != null) {
                        Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
                        try {
                            addresses = geocoder.getFromLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude(), 1);

                            if (addresses != null && addresses.size() > 0) {
                                Log.i("CURRENT LOCATION", addresses.get(0).getLocality() + ", " + addresses.get(0).getCountryName());
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.e("Getting current loc err", "Error at getting current location..");
                        }

                    } else {

                        //request for updated location
                        final LocationRequest locationRequest = LocationRequest.create();
                        locationRequest.setInterval(1000);
                        locationRequest.setFastestInterval(1000);
                        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

                        locationCallback = new LocationCallback() {
                            @Override
                            public void onLocationResult(LocationResult locationResult) {
                                super.onLocationResult(locationResult);
                                if (locationResult == null) {
                                    return;
                                }
                                mLastLocation = locationResult.getLastLocation();
                                mFusedLocationClient.removeLocationUpdates(locationCallback);
                            }
                        };

                        mFusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
                    }
                } else {
                    //task was not successful
                    Log.e("NOT SUCCESSFUL", "Unable to get location");
                }
            }
        });
    }
Kate
  • 163
  • 1
  • 1
  • 8

0 Answers0