0

I am trying to get the latitude and longitude from a service and I am getting null values for both latitude and longitude.This is the code that I am trying to implement:-

public class MyService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    public static Location mCurrentLocation;
    Location mLastLocation;
    public Double latitude, longitude;

    @Override
    public void onCreate() {
        //show error dialog if GoolglePlayServices not available
        if (isGooglePlayServicesAvailable()) {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(10000);
            mLocationRequest.setFastestInterval(5000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setSmallestDisplacement(0f);  /* min dist for location change, here it is 10 meter */
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent, flags, startId);
    }

    //Check Google play is available or not
    private boolean isGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        return ConnectionResult.SUCCESS == status;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {


    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
                if (mLastLocation != null) {
                    latitude = mLastLocation.getLatitude();
                    mLastLocation.getLongitude();
                }

        Toast.makeText(this, "LatLang " + latitude + " " + longitude, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onConnectionSuspended(int i) {

            }

            @Override
            public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

            }
        }

I am new to programming so any help or suggestion is appreciated.Thank you.

AndroidNewBee
  • 744
  • 3
  • 12
  • 36
  • You should use runtime permission correctly for android api level 6.0 and above, follow http://stackoverflow.com/questions/40783020/android-6-0-getlastknowlocationpermission/40783661#40783661 – Ready Android Nov 29 '16 at 06:16
  • @ReadyAndroid I am not testing it on 6.0 I amusing it on 4.4(KitKat) – AndroidNewBee Nov 29 '16 at 06:18
  • @AndroidNewBee You will get latitude and longitude value in `onLocationChanged()` method.Now you are trying to get last location which might be not saved in your device because of that you are getting null values – Anil Ravsaheb Ghodake Nov 29 '16 at 07:16
  • @AndroidNewBee Please check this Fused Listener Example for more details.Let me know if gets any error – Anil Ravsaheb Ghodake Nov 29 '16 at 07:31

0 Answers0