11

i am trying to get the users location for my new navigation app. i want to check the users location frequently and it has to be accurate . I use the following code from sample to get the location .

public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private String provider;
private TextView a;
private TextView b;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a = (TextView) findViewById(R.id.a);
    b = (TextView) findViewById(R.id.b);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
    } else {
        a.setText("Location not available");
        b.setText("Location not available");
    }
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider,0, 1, this);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    Double lat =  location.getLatitude();
    Double lng =  location.getLongitude();
    a.setText(String.valueOf(lat));
    b.setText(String.valueOf(lng));
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}
}

But location cordinates are not updating as i move,i get a constant value always ... is there any better way to get location ? According to to android docs i can use Google Play Location Service or Android platform location APIs for this . I checked the sample code for both and it implements the LocationListener interface to get the user location .So whats the actual difference between them?

2
  • set criteria.setAccuracy(Criteria.ACCURACY_COARSE); and try
    – M D
    Jan 28, 2014 at 5:02
  • the Gps provider doesn't work properly in Indoor...it works correctly in outdoor... Jan 28, 2014 at 5:40

2 Answers 2

23

Absolutely use the new google play location services. They work much better indoors than the old location manager. I have created a sample that uses the new google play location services and works in the background periodically. Check it out:

https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android

3
  • 4
    Remember that if you use Google Location, your app now depends on the Google Play Service which is closed source own by google. Therefor, your app won't work with AOSP Android device.
    – Nico
    Sep 8, 2015 at 22:00
  • Hey man, you could take me a question about your project on GitHub? It just sends the moment you open the application or even without opening the application, when the boot in the cell it begins also send the locations? Nov 5, 2015 at 4:19
  • 1
    Does , Google play location API requires network connectivity ?
    – charany1
    Feb 18, 2016 at 7:56
3

When I was working with it, I came across 2 good sources:

  1. The blog post: Android Location Fused Provider. It includes a great complete tutorial on using the FusedLocationProviderApi to get location in Android.

  2. Answers on following Android LocationClient class is deprecated but used in documentation question.

Basically both talk about the similar steps as mentioned in the Google I/O session, link to which is mentioned in one of the answers:

  • First, to get a GoogleClientApi instance, like:

    GoogleClientApi googleApiClient = new GoogleApiClient.Builder(locationActivity)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    
  • Then to call .connect() on it like:

    googleApiClient.connect();
    
  • Then in the callback onConnected, get the last location or even can request the location updates as per your need, eg:

    LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    

    If you want request location updates, then can use the requestLocationUpdates call.

Hope it helps.

1
  • What if getting periodic update of GPS Location ? If I have set the setting setInterval as 2000 ms , how shall I test ? indoor office or outside yard ? Oct 14, 2016 at 6:24

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.