LocationClient requestUpdates onLocationChanged never called
Asked Answered
H

1

6

I have this code which I use to position a map app. I tested it in a Galaxy S2 device and works fine, but when I tested it in a nexus 4 device the onLocationChanged callback never gets called. What could I be doing wrong?

Here is my activity code:

    public class BaseMapActivity extends FragmentActivity implements Injectable,
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

    private MapEntity component;
    private boolean yetStarted = false;

    private LocationClient mLocationClient;
    private LocationRequest mLocationRequest;
    private Location location;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.map_activity);
        mLocationClient = new LocationClient(this, this, this);
        mLocationRequest = LocationRequest.create();

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // TODO
        mLocationRequest.setInterval(5000); // TODO
        mLocationRequest.setFastestInterval(5000); // TODO

        replaceFragment(R.id.map_container, mapFragment);

        // Agrega el fragment de extras
        Fragment extrasFragment = component.getExtrasFragment();
        replaceFragment(R.id.map_extras, extrasFragment);
    }

    @Override
    protected void onStart() {
        super.onStart();

            mLocationClient.connect();
    }

    @Override
    protected void onStop() {

        if (mLocationClient.isConnected()) {
            mLocationClient.removeLocationUpdates(this);
            mLocationClient.disconnect();
        }

        super.onStop();
    }

    @Override
    public void onConnected(Bundle arg0) {

        mLocationClient.requestLocationUpdates(mLocationRequest, this);
        location = mLocationClient.getLastLocation();

        if (location == null) {
            return;
        }

        Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDisconnected() {
        mLocationClient.removeLocationUpdates(this);
        Toast.makeText(this, "onDisconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationChanged(Location location) {
        component.setCenter(location);
        String msg = "Updated Location: "
                + Double.toString(location.getLatitude()) + ","
                + Double.toString(location.getLongitude());
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {

        case CONNECTION_FAILURE_RESOLUTION_REQUEST:

            switch (resultCode) {
            case Activity.RESULT_OK:
                break;
            }
        }
    }

    @SuppressLint("ShowToast")
    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this,
                        CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "An error ocurred getting current location",
                    Toast.LENGTH_SHORT);
        }

    }

}

Whats weird is that its working in one device but not in the other.

Huggermugger answered 25/7, 2013 at 13:44 Comment(1)
Could be an issue with the device, this may be relevant #9008100Commissioner
N
0

Try to set location mock up to see if the locationChange is called or not.

Here you have an example i was using for this before production.

private Location createMockLocation() {
    if (mockLocation == null) {
        mockLocation = new Location("mock");
        mockLocation.setLatitude(41.4934133);
        mockLocation.setLongitude(-23.8729252);
        mockLocation.setAccuracy(1.0f);
        mockLocation.setTime(new Date().getTime());
    } else {
        mockLocation.setLatitude(mockLocation.getLatitude() + 0.00003);
        mockLocation.setLongitude(mockLocation.getLongitude() + 0.00001);
        mockLocation.setTime(new Date().getTime());
    }
    return mockLocation;
}

private void mockLocation() {
    LocationServices.FusedLocationApi.setMockMode(clientApi, true);
    LocationServices.FusedLocationApi.setMockLocation(clientApi, createMockLocation());

    final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.scheduleAtFixedRate(new Runnable() {
        public void run() {
            LocationServices.FusedLocationApi.setMockLocation(clientApi, createMockLocation());
            // Log.d("MockLocation",
            // "mockLocation: Lat:"+mockLocation.getLatitude()+" Long:"+mockLocation.getLongitude());
        }
    }, 0, NORMAL_INTERVAL, TimeUnit.MILLISECONDS);
}

With this method you get an update position each NORMAL_INTERVAL in ms.

Hope this helps :).

Naturism answered 7/4, 2015 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.