LocationManager vs Google Play Location Services

Client Location on Android

Getting the client’s area on Android is somewhat less clear than on iOS. To begin the disarray, there are two entirely unexpected ways you can do it. The first is utilizing Android APIs from android.location.LocationListener, and the second is utilizing Google Play Services APIs com.google.android.gms.location.LocationListener. We should experience them two.

Android’s Location API

The Android’s area APIs utilize three distinct suppliers to get area –

  1. LocationManager.GPS_PROVIDER — This supplier decides area utilizing satellites. Contingent upon conditions, this supplier may require a long time to restore an area settle.
  2. LocationManager.NETWORK_PROVIDER — This supplier decides area in light of accessibility of cell tower and WiFi get to focuses. Results are recovered by methods for a system query.
  3. LocationManager.PASSIVE_PROVIDER — This supplier will return areas created by different suppliers. You inactively get area refreshes when different applications or administrations ask for them without really asking for the areas yourself.

The significance of it is that you get a question of LocationManager from the framework, actualize the LocationListener, and call the requestLocationUpdates on the LocationManager.

Here’s a code bit:

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Google’s Location Services API

Google’s Location Services API is a piece of the Google Play Services APK (here’s the manner by which to set it up) . They’re based over Android’s API. These APIs give a “Melded Location Provider” rather than the suppliers specified previously. This supplier consequently picks what lace bridesmaid dresses basic supplier to utilize, in view of exactness, battery utilization, and so forth. It is quick since you get area from a framework wide administration that continues refreshing it. What’s more, you can utilize more propelled elements, for example, geofencing.

To utilize the Google’s Location Services, your application needs to associate with the GooglePlayServicesClient. To associate with the customer, your action (or piece, or somewhere in the vicinity) needs to execute GooglePlayServicesClient.ConnectionCallbacks and GooglePlayServicesClient.OnConnectionFailedListener interfaces. Here’s an example code:


public class MyActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
LocationClient locationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
locationClient = new LocationClient(this, this, this);
}
@Override
public void onConnected(Bundle bundle) {
Location location = locationClient.getLastLocation() ;
Toast.makeText(this, "Connected to Google Play Services", Toast.LENGTH_SHORT).show();
}
@Override
public void onDisconnected() {
Toast.makeText(this, "Connected from Google Play Services.", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// code to handle failed connection
// this code can be found here — http://developer.android.com/training/location/retrieve-current.html
}

Google’s API Guide on Location Strategies clarifies the code pretty pleasantly. Yet, they likewise say that much of the time, you’ll show signs of improvement battery execution, and also more fitting precision, by utilizing the Google Location Services API. Presently the perplexity begins!

Be the first to comment

Leave a Reply

Your email address will not be published.


*