0

mLastLocation: null, always is null, how can solve it?

this is my code

private void displayLocation() {
        if(ActivityCompat.checkSelfPermission(TrackingOrder.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(TrackingOrder.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            Log.d("Check-1","ok");
            requestRuntimePermission();
        }
        else
        {
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            //mLastLocation = LocationServices.getFusedLocationProviderClient(this).getLastLocation().addOnSuccessListener(mGoogleApiClient);
            Log.d("Check-2","ok");
            Log.d("mLastLocation",String.valueOf(mLastLocation));
            if(mLastLocation != null)
            {

                double latitude = mLastLocation.getLatitude();
                double longitude = mLastLocation.getLongitude();
                Log.d("Check - latitude",String.valueOf(latitude));
                Log.d("Check - longitude",String.valueOf(longitude));

                // Add Marker in your location and move the camera
                LatLng yourLocation = new LatLng(latitude,longitude);
                mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(yourLocation));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
                
                //After add marker for your location, add marker for this order and draw route
                drawRoute(yourLocation,Common.currentRequest.getAddress());
                
            }
            else
            {
                Log.d("Error","Cannot get location");
                Toast.makeText(this,"Could'nt get your location", Toast.LENGTH_SHORT).show();
            }
        }
    }

this is my mGoogleApiClient code

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API).build();

        mGoogleApiClient.connect();
    }

But when I locad this displayLocation funcation, there is problem

mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

Because the mLastLocation still get null.

So where I need to modify?

there has full code

https://luvtas.com/gmap.txt

Stanley
  • 45
  • 5

1 Answers1

0

There are some edge cases when getLastLocation returns null. Look at this question for more information: LocationClient getLastLocation() return null

Are you testing on emulator or physical device? Do you have GPS turned on? Do you have permissions defined in Manifest.xml?

Also, in your code, you are listening for location updates in onLocationChanged(Location location). You are storing new received location in mLastLocation and then calling displayLocation(). And then you do mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); in displayLocation() and override value received from onLocationChanged. Maybe not intended?

Kubik
  • 610
  • 7
  • 15
  • I'd turn on GPS on the emulator device. And I have defined permission in Manifest.xml. – Stanley Aug 28 '20 at 08:01
  • OK, I assume you have set GPS location manually for Emulator: https://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator . Then, try to launch Google Maps app on Emulator before your application. It seems to help according to this: https://stackoverflow.com/questions/19090573/locationclient-getlastlocation-always-returns-null-on-emulator Hope this helps. – Kubik Aug 28 '20 at 08:29
  • the app is working, but just cannot get mLastLocation values – Stanley Aug 28 '20 at 10:06
  • Yes, I understand that app is working. But emulator doesn't know its GPS location. You have to set it manually, either via telnet command or via emulator menu. Have you done that? – Kubik Aug 28 '20 at 10:18
  • I have manually setting, but the mLastLocation data from Firebase, it should be has get date – Stanley Aug 29 '20 at 05:45