Google Play Services or Android Search Services

I want to implement location functionality in my application. I have read a little and I find myself a little puzzled.

When searching through the tutorial, almost every result is returned with an example using the Android Location API.

However, reading the Android developer guides states the following:

Location APIs of Google Play services are preferred over Android Location APIs (android.location) as a way to add location information to an application. If you are using the Android Location APIs, you are strongly encouraged to switch to the Google Play Services Location APIs as soon as possible.

Android docs

So this is telling me not to go down the easier path by simply implementing a location receiver.

So my question is, what is the difference between the two? Why should I use one and not the other?

Where can I find a decent tutorial on how to correctly and accurately access the Google Play Services Location API.

I've tried this so far (as suggested on the android site) however none of my callbacks get called.

public class LocationManager implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private Context mContext;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;

public LocationManager(Context context) {
    mContext = context;
    //
    if (checkIfGooglePlayServicesAreAvailable()) {
        //Get Access to the google service api
        buildGoogleApiClient();
    } else {
        //Use Android Location Services
        //TODO:
    }
}

public Location getCoarseLocation() {
    if (mLastLocation != null) {
        return mLastLocation;
    } else return null;
}

private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(mContext)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

private boolean checkIfGooglePlayServicesAreAvailable() {
    int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (errorCode != ConnectionResult.SUCCESS) {
        GooglePlayServicesUtil.getErrorDialog(errorCode, (MainActivity) mContext, 0).show();
        return false;
    }
    return true;
}


@Override
public void onConnected(Bundle bundle) {
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location != null) {
        mLastLocation = location;
        Toast.makeText(mContext, location.getLongitude() + " , " + location.getLatitude() + " : " + location.getAccuracy(), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(mContext, "suspended", Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Toast.makeText(mContext, connectionResult.toString(), Toast.LENGTH_LONG).show();
}
}

      

Then I call my Location dispatcher from my activity:

LocationManager locationManager = new LocationManager(this);
Location location = locationManager.getCoarseLocation();
//Use Location

      

I want to create a helper class that I can simply call from any activity or fragment. However, when I run the following, the constructor succeeds. However, none of my breakpoints in the callbacks hit. even after 1 or 2 minutes.

+3


source to share


2 answers


The Google Play Services Api uses a callback method public void onConnected(Bundle bundle)

that is only called when a connection is established. Only then LocationServices.FusedLocationApi.getLastLocation(googleApiClient)

can the method call return the correct one Location

.

You've created a helper class to get this location, but you must understand that the connection is not immediately established. Therefore, simply calling the helper class asynchronously or its method may return a null object, since the connection might not have been made.

You can get in the Location

following ways.

1) Pass the location and receive it through BroadcastReceiver

for subsequent operations from public void onConnected(Bundle bundle)

. This worked for me.

private boolean flag = false;

@Override
public void onConnected(Bundle connectionHint) {
    location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null && !flag) {
        flag = true;
        Intent intent = new Intent(LOCATION_BROADCAST_ACTION);
        intent.putExtra(INTENT_EXTRA_LOCATION, location);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
        Log.i(TAG, "Sending Location Broadcast");
        Log.i(TAG, location.toString());
    } else if (location == null){
        Toast.makeText(mContext, R.string.no_location_detected, Toast.LENGTH_LONG).show();
        Log.e(TAG, "No Location Detected");
    }
}

      



2) Create a public object Location

in the calling activity or fragment and update its value from the method public void onConnected(Bundle bundle)

.

@Override
public void onConnected(Bundle connectionHint) {
    location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    if (location != null) {
        MyActivity.location = location; // update the Location object of you caller activity or fragment
        Log.i(TAG, "location updated");
    }
}

      

Also, you have to connect to Google Play Services Location Api after creating GoogleApiClient. Just add the method mGoogleApiClient.connect()

call after the method call buildGoogleApiClient();

to your constructor. You don't need to check if Google Play services are available or not.

You can also add a method call googleApiClient.connect();

to your implementation public void onConnectionSuspended(int i)

.

+5


source


You need to call:

mGoogleApiClient.connect()

      



In addition to your question about the differences, the Google Play services framework provides you with a fused location. With Android map, you must use GPS or Wif. You can specify the criteria, but if you want the position of the location user, the best google play services are the best choice. If you need a very precise position like GPS because, for example, your app is a navigation app, than it is better to use GPS directly.

+2


source







All Articles