Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unclear Android Current Location Retrieval Tutorial

Tags:

android

I am recycling the code provided to check that the user's device has Google Play services on it before acquiring location data from http://developer.android.com/training/location/retrieve-current.html. When copy-pasting it into my IDE, Eclipse correctly points out errors in lines, because "connectionResult" has never been defined, nor is "getSupportFragmentManager"

int errorCode = connectionResult.getErrorCode();

and

errorFragment.show(getSupportFragmentManager(),
                    "Location Updates");

Should I just create a variable above called ConnectionResult connectionResult to fix the issue? I am not sure about how to correct the second.

Additionally, the line

mLocationClient = new LocationClient(this, this, this);

from further along the page suggests putting in the MainActivity class which does not satisfy the LocationClient constructor, evoking another error.

Update: Another issue with the tutorial. Hi all, the tutorial references the class LocationResult which it has not created here: http://developer.android.com/training/location/receive-location-updates.html. How/where am I supposed to define this?

like image 563
NumenorForLife Avatar asked May 31 '13 05:05

NumenorForLife


1 Answers

The tutorial is misleading. If you want to check google play services exist do the following.

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS) {
  GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}

This will automatically show an appropriate error dialog if it doesn't in exist.

To your second problem. The remainder of the tutorial does need to be followed. You do need to implement GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener if you want to create the the locationclient using new LocationClient(this, this, this);

Note: do not try to use the locationclient until after the onConnected method is called in your callback.

like image 183
Lionel Port Avatar answered Nov 16 '22 01:11

Lionel Port