1

I tried an app to show latitude and longitude by using Google Play Services, and its working fine in below Lollipop versions. But it shows "location not detected" on Lollipop. I used following code:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private Location mLocation;
private static final String TAG = "MainActivity";
private TextView mLatitudeTextView;
private TextView mLongitudeTextView;
Geocoder geocoder;
List<Address> addresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview));
    mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview));
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

@Override
public void onConnected(Bundle bundle) {

    mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLocation != null) {
   mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
          mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));

    } else {
        Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onConnectionSuspended(int i) {

    Log.i(TAG, "Connection Suspended");
    mGoogleApiClient.connect();
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

    Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
}

And I included compile com.google.android.gms:play-services:8.1.0 in gradle, and android.permission.ACCESS_COARSE_LOCATION in manifest file.

Please anyone help me to solve this issue.

Floern
  • 33,559
  • 24
  • 104
  • 119
jasil
  • 79
  • 1
  • 2
  • 11

2 Answers2

0

Try to add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />.
If not try using this tutorial: http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

Lotus91
  • 1,127
  • 4
  • 18
  • 31
  • make sure everything is included correctly in your manifest file. – Lotus91 Mar 04 '16 at 10:36
  • i tried this tutorial http://www.sitepoint.com/google-play-services-location-activity-recognition/ . that says Fine Location uses the device GPS, cellular data and WiFi to get the most accurate position but it costs battery life. Coarse Location uses the device cellular data and WiFi to get the location. It wont be as accurate as Fine but uses a lot less battery power, returning a location with an accuracy equivalent to a city block. – jasil Mar 04 '16 at 12:12
0

Use this method to connect to Google API Client:

GoogleApiClient apiClient=null;
LocationRequest mLocationRequest=null;

private void setLocationLocationRequest() {

        try {
            apiClient=new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(29000);
            mLocationRequest.setFastestInterval(5000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            apiClient.connect();

        }catch (Exception e){
            Log.d("LS", e.getMessage() == null ? "" : e.getMessage());
        }

    }

For more details , check this Answer and this answer

Community
  • 1
  • 1
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87