1

I have the following simple class, which is my main activity. I essentially followed this verbatim from the Android docs. I am running this from my phone through Android Studio. The problem I am running into is I absolutely never get a legitimate location out of this setup. The location object is always null. In reality, I want the location immediately in a similar fashion to how a browser quickly grabs the location through navigator.geolocation.

Is there something weird with the way I have this set up? If not, how do I force a location out of the Fused Location Provider API?

public class MyActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    private Location location;
    private GoogleApiClient mGoogleApiClient;
    private static LocationRequest locationRequest;

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

        // Create an instance of GoogleAPIClient.
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    }

    @Override
    protected void onStart() {
        mGoogleApiClient.connect();
        super.onStart();
    }

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

    private void requestLocationUpdates() {
        locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(1000);
        locationRequest.setFastestInterval(500);
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        requestLocationUpdates();
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,  locationRequest, this);
        location = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }

    @Override
    public void onConnectionSuspended(int i) {}

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
}
zebra
  • 6,373
  • 20
  • 58
  • 67

1 Answers1

0

High accuracy means its going to require GPS. If you can't actually contact the GPS satellies (for example, you're inside a building without a clear line to the sky) then it will never send a result. This is actually correct behavior- you wanted high accuracy, its waiting until you have it.

If you're ok with less accuracy, use a different priority type. Otherwise you'll have to accept that you may never get a response.

DO expect it to take a few seconds even under optimal conditions, BTW. It has to find a half dozen sattelites in space before it can give an answer, that takes time.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for your reply. I changed to `PRIORITY_LOW_POWER` and the same result, even after letting the application run for 20 seconds before trying anything. It is surprising to me that this is difficult, since browsers have no problem picking up location immediately – zebra Jul 05 '16 at 15:35