Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocationClient getLastLocation() returning null

I'm new to Android programming and wanted to start by creating a very basic app that displays the latitude and longitude of the current location on screen.

I'm developing for my Samsung Galaxy S3 and read that you need to kickstart the phone to return the GPS.

I've tried doing this in the onConnected() method with the requestLocationUpdates() method call on the LocationManager. However I find that the LocationClient is still returning a null location.

Can anyone tell me what I'm doing wrong?

Here's my MainActivity:

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;

public class MainActivity extends FragmentActivity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener {

    private LocationClient mLocationClient;
    private Location mCurrentLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLocationClient = new LocationClient(this, this, this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationClient.connect();
    }

    @Override
    protected void onStop() {
        mLocationClient.disconnect();
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onConnected(Bundle arg0) {
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        LocationManager manager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
                new LocationListener() {
                    @Override
                    public void onStatusChanged(String provider, int status,
                            Bundle extras) {
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                    }

                    @Override
                    public void onLocationChanged(final Location location) {
                    }
                });
        mCurrentLocation = mLocationClient.getLastLocation();

        TextView latTextView = (TextView) findViewById(R.id.latitude_text);
        latTextView.setText(Double.toString(mCurrentLocation.getLatitude()));

        TextView longTextView = (TextView) findViewById(R.id.longitude_text);
        longTextView.setText(Double.toString(mCurrentLocation.getLongitude()));
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

}

My Android manifest looks like this:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />


    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.willigetwet.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

When running the debugger I find that mCurrentLocation is null after the call to mLocationClient.getLastLocation().

I'd be very grateful for any help!

like image 663
Sarah Tattersall Avatar asked Jul 27 '13 07:07

Sarah Tattersall


People also ask

How to get last location in android programmatically?

Once you have created the Location Services client you can get the last known location of a user's device. When your app is connected to these you can use the fused location provider's getLastLocation() method to retrieve the device location.

What is fused location client?

The fused location provider is a location API in Google Play services that intelligently combines different signals to provide the location information that your app needs.


1 Answers

mLocationClient.getLastLocation(); might return null if you don't have any last location saved by the device. If you read its documentation, it says;

It returns the best most recent location currently available. If a location is not available, which should happen very rarely, null will be returned

Also inside the onLocationChanged method you might want to set the Location to the latest location, when your location gets changed.

@Override
public void onLocationChanged(final Location location) {
      mCurrentLocation = location;
}
like image 106
Shobhit Puri Avatar answered Oct 02 '22 03:10

Shobhit Puri