1

I've implemented the fused location api in my code. I can clearly see the changes of latitude and longitude for every required (Priority High accuracy,Interval- 1sec,fastest interval -1s) seconds in my logs. But the getSpeed() is always returning 0.0 in motoG (marshmallow). But the similar code works fine in Lenovo(marhsmallow).

While exploring this issue, articles stated difficulties in locking agps in motoG.But in my case lat and long update is fine which means locking is considerably good. :(

Tried different settings, nothing works. I need solution regarding this issue from someone who had overcome this. Thanks in advance.

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

private static final String TAG = "MainActivity";
private static final int REQUEST_LOCATION_PERMISSION = 1;
private boolean mPermissionApproved;
private GoogleApiClient mGoogleApiClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPermissionApproved = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
    if (!mPermissionApproved) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_PERMISSION);
    }
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
protected void onPause() {
    super.onPause();
    if ((mGoogleApiClient != null) && (mGoogleApiClient.isConnected()) &&
            (mGoogleApiClient.isConnecting())) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

@Override
protected void onResume() {
    super.onResume();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    requestLocation();
}

private void requestLocation() {

    if (mPermissionApproved) {
        LocationRequest locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(0)
                .setFastestInterval(0);

        LocationServices.FusedLocationApi
                .requestLocationUpdates(mGoogleApiClient, locationRequest, this)
                .setResultCallback(new ResultCallback<Status>() {
                    @Override
                    public void onResult(Status status) {
                        if (status.getStatus().isSuccess()) {
                            if (Log.isLoggable(TAG, Log.DEBUG)) {
                                Log.d(TAG, "Successfully requested location updates");
                            }
                        } else {
                            Log.e(TAG,
                                    "Failed in requesting location updates, "
                                            + "status code: "
                                            + status.getStatusCode() + ", message: " + status
                                            .getStatusMessage());
                        }
                    }
                });
    }
}

@Override
public void onConnectionSuspended(int i) {

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
            Toast.makeText(this,"latitude::" + location.getLatitude()+" longitude::"+ location.getLongitude()+ " Speed::" + location.getSpeed(), Toast.LENGTH_SHORT).show();
}

@Override
public void onRequestPermissionsResult( int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    if (requestCode == REQUEST_LOCATION_PERMISSION) {

        if ((grantResults.length == 1)
                && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {

            mPermissionApproved = true;
            if(mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
                requestLocation();
            }
        } else {
            mPermissionApproved = false;
        }
    }
}
} 
lokusking
  • 7,396
  • 13
  • 38
  • 57
  • 1
    I see this is you first Question on Stack Overflow, welcome! Please provide some code so we can see what you have tried and better understand the context of your question, which in turn will help us out to help you. Also see [How To ask](http://stackoverflow.com/help/how-to-ask) on how to ask better question which lead to better answers. – Raymond Jan 16 '17 at 13:28
  • 1
    Thanks a lot. Yes,am a beginner. Included the basic code snippet. OnLocationChange() within the toast am able to get the lat and long difference but the speed is always returning 0.0 incase of motoG(marshmallow). same sample application works fine, provides speed in lenovo (marshmallow). @RaymonddelaCroix – Akshaya Anilkumar Jan 17 '17 at 12:52

0 Answers0