1

I'm trying to create an Android Location API using Google Play Services. But I keep on getting " mLastLocation value as null " with a message " Couldn't get the location. Make sure location is enabled on the device " which I have given to display for the null value of mLastLocation. Can anyone tell me what I am doing wrong? Here's the code which I have been trying. Thanks in Advance.

CODE:

package com.example.jamshi.locationapi;

import android.app.Activity;
import android.location.Location;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

 public class MainActivity extends Activity implements ConnectionCallbacks,OnConnectionFailedListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
    private Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;

    private TextView tvLocation;
    private Button btnShowLocation,btnLocationUpdates;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvLocation = (TextView) findViewById(R.id.tvLocation);
        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
        btnLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates);

        if(checkPlayServices()){
            buildGoogleApiClient();
        }

        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                displayLocation();
            }
        });

    }

    private void displayLocation() {

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            double latitude = mLastLocation.getLatitude();
            double longitude = mLastLocation.getLongitude();
            tvLocation.setText(latitude + ", " + longitude);

        } else {

            tvLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
        }
    }

    private boolean checkPlayServices(){

        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if(resultCode != ConnectionResult.SUCCESS){
            if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){
                GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show();
            }else{
                Toast.makeText(getApplicationContext(),"This device is not supported",Toast.LENGTH_LONG).show();
                finish();
            }
            return false;
        }
        return true;
    }

    protected synchronized void buildGoogleApiClient() {

        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API).build();
        }
    }

    @Override
    protected void onStart() {

        if (mGoogleApiClient != null) {
            mGoogleApiClient.connect();
        }
        super.onStart();
    }

    @Override
    protected void onResume() {

        super.onResume();
        checkPlayServices();
    }


    @Override
    public void onConnectionFailed(ConnectionResult result) {

        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
                + result.getErrorCode());
    }

    @Override
    public void onConnected(Bundle arg0) {

        displayLocation();
    }

    @Override
    public void onConnectionSuspended(int arg0)
    {
        mGoogleApiClient.connect();
    }

}

2 Answers2

0

If you are running on emulator then open google maps application first and then try your app

  • I am testing in emulator...................I have opened google maps application first and again the same error repeats............ – user6239257 Apr 29 '16 at 13:54
  • Try adding this code in **displayLocation()** at top. **mLocationRequest = LocationRequest.create();** **mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);** and dont forget to declare the variable **private LocationRequest mLocationRequest;** – Zain ul abdeen Apr 29 '16 at 19:22
0

Test in device if you want to get data in first time and for genymotion emulator set location from side menus and then try. and if you are using android studio emulator set mock location and then try.

For more check this - How to emulate GPS location in the Android Emulator?

Community
  • 1
  • 1
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55