1

I am developing app which require location services my code is as follow. The main problem is on location changed never gets called

public class MainActivity extends ActionBarActivity implements
    ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
protected GoogleApiClient mGoogleApiClient;
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 100;
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
        UPDATE_INTERVAL_IN_MILLISECONDS / 2;
protected LocationRequest mLocationRequest;
protected static final String TAG = "location-updates-sample";
protected Location mCurrentLocation;

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


    btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i(TAG,"btn is clicked");
            buildGoogleApiClient();
            Log.i(TAG,"build google api completed");
            mGoogleApiClient.connect();
            Log.i(TAG,"client conneted");

        }
    });
}

protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    createLocationRequest();
}
protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
 mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
 mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
 LocationServices.FusedLocationApi.requestLocationUpdates(
     mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnected(Bundle bundle) {
    startLocationUpdates();
    Log.i(TAG,"from onconnected method");
}

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

}

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    Log.i(TAG,"on location changed");
    Log.i(TAG,String.valueOf(mCurrentLocation.getLongitude()));
    Log.i(TAG,String.valueOf(mCurrentLocation.getLatitude()));
    Toast.makeText(this,"liocation changed", Toast.LENGTH_SHORT).show();

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

I have done "ACCESS_FINE_LOCATION" in manifest. also set the setting to be ''gps'' device only. i am getting the result when i click on button

location-updates-sample﹕ btn is clicked
location-updates-sample﹕ Building GoogleApiClient
location-updates-sample﹕ build google api completed
location-updates-sample﹕ client conneted
location-updates-sample﹕ from onconnected method

whats wrong with my code

3 Answers3

0

I use the same code to handle locations. Don't know if it's the correct solution but try to replace

mLocationRequest = new LocationRequest();

with

mLocationRequest = LocationRequest.create();

Works for me.

Niccolò Passolunghi
  • 5,966
  • 2
  • 25
  • 34
0

I don't know english. Hope i can help:

I think your UPDATE_INTERVAL_IN_MILLISECONDS = 100( 0,1 s) and FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS ( 100/ 2 = 0.05s) is very fast with gps chip . you should set it > 1s . some thing like this

UPDATE_INTERVAL_IN_MILLISECONDS  =  10 * 1000;// 10s

FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS  = 5 *1000; // 5s
YenMinh
  • 509
  • 5
  • 12
  • not working i am following this tutorial https://github.com/googlesamples/android-play-location/blob/master/LocationUpdates/app/src/main/java/com/google/android/gms/location/sample/locationupdates/MainActivity.java – shivashankar bhutekar Feb 03 '15 at 13:29
0

Make Sure you have added .addApi(LocationServices.API)

I was not getting onLocationChanged(Location location) call in my emulator.

current_location =LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

Using this can find current location.

Hope this will help you.

Jogendra Gouda
  • 405
  • 4
  • 17