Roland
Roland

Reputation: 876

How to get the actual location?

I have an app that has to get the actual latitude and longitude. I am trying to use the code posted here to do so: How do I get the current GPS location programmatically in Android? The class GPSTracker.

The problem is that sometimes it is too slow to get the location and it sometimes gets a outdated location. I don't need to keep tracking the location, I just need to get the location when the user presses a button. The maps app from Google can get it so fast, how can I do that?

Upvotes: 1

Views: 661

Answers (3)

Atish Agrawal
Atish Agrawal

Reputation: 2877

i may not be correct about this, as i am a newbie too in android.

But according to me there are three different ways by which one can find users location.

  1. Using wi-fi provider
  2. Using Network provider
  3. Using GPS provider

getting data from GPS is very slow as compared to rest.

What you can do is, check for the network availability first and if it is present, fetch the latitude and longitude from there.

Later you can use GPS for more accurac. but initially, getting location from the network is much faster

Upvotes: 0

nPn
nPn

Reputation: 16728

I believe they use this "new" location api. There is sample code you can here as well to get you up and running. I have used this in an app I am working on and it works really well. I used the "balanced" mode.

http://developer.android.com/google/play-services/location.html

Upvotes: 0

Rachit Mishra
Rachit Mishra

Reputation: 6112

How about using the newly introduced Fused location provider as referenced from: http://developer.android.com/training/location/retrieve-current.html

public static class MyActivity extends Activity
            implements
                GooglePlayServicesClient.ConnectionCallbacks,
                GooglePlayServicesClient.OnConnectionFailedListener,
                LocationListener {
        private LocationRequest lr;
        private LocationClient lc;
        Location location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // inflate view
        lr = LocationRequest.create();
        lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        lc = new LocationClient(this,
                this, this);
        lc.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
       //Get new Location here.
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        lc.requestLocationUpdates(lr, this);
        // get last Location
        location = lc.getLastLocation();
    }

    @Override
    public void onDisconnected() {

    }
}

Upvotes: 1

Related Questions