1

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?

Community
  • 1
  • 1
Roland
  • 826
  • 2
  • 9
  • 24

3 Answers3

1

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() {

    }
}
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
  • What is this GooglePlayServicesClient? All cell phones have this? Why I have to check it before? – Roland Sep 09 '13 at 12:45
  • Google Play Services are now available on almost all devices, still there may be chance user haven't updated it or installed it, so we need to check for it, it simplifies many of things like getting the location much easily, more info here http://developer.android.com/google/play-services/index.html – Rachit Mishra Sep 09 '13 at 12:49
  • Even the old cellphones have the Google Play Services? The aplication will not work if this is not instaled? – Roland Sep 09 '13 at 12:56
  • @Roland Play Service are available on api 8 and above phones, and yup without this services, your app will give errors, so better check for play services before beginning to use the app, which is not that difficult. – Rachit Mishra Sep 09 '13 at 12:59
  • Ok, so its comes installed in api 8 and above (froyo)? Thanks for the answers. I will test this class later night today. If work faster will will let you know. Thak you very much. – Roland Sep 09 '13 at 13:05
  • @Roland the new maps api uses this class only, it is slowly being installed by google on all phones. okay. :) – Rachit Mishra Sep 09 '13 at 13:06
  • I tried to implement this class but its not working. I am not able to implemet "GooglePlayServicesClient". It give me an error "GooglePlayServicesClient cannot be resolved to a type". – Roland Sep 10 '13 at 12:06
  • @Roland you need to add GooglePlayServices library to your project. – Rachit Mishra Sep 10 '13 at 12:11
  • I know, I trying, but I not getting. – Roland Sep 10 '13 at 12:33
  • http://twntee.tumblr.com/post/59576020346/the-maps-api-adding-a-map-to-your-android-project see this ! – Rachit Mishra Sep 10 '13 at 12:34
  • Hi, @twntee. I used your class but it ask me to implement com.google.android.gms.location.LocationListener. Then I did it, but when I get the latitude and longitude with location.getLatitude() it return me 0.0. – Roland Sep 11 '13 at 12:15
  • I tried to use this class but the location is returning null. – Roland Sep 16 '13 at 13:38
  • Its working now, the problem was with the library. Thank you very much for the help. – Roland Sep 16 '13 at 17:00
  • @Roland you are welcome, sorry I was very busy, so I am not able to reply. :) – Rachit Mishra Sep 17 '13 at 10:34
0

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

nPn
  • 16,254
  • 9
  • 35
  • 58
  • I did not understood very well, the cell phone must have instaled the google play service? And what is that? – Roland Sep 09 '13 at 12:36
  • from the above page. "Although these Google services are not included in the Android platform, they are supported by most Android-powered devices. When using these services, you can distribute your app on Google Play to all devices running Android 2.2 or higher, and some services support even more devices." – nPn Sep 09 '13 at 12:50
  • so you really should check (in your code) that google services are installed. – nPn Sep 09 '13 at 12:51
  • I tried to implement this class but its not working. I am not able to implemet "GooglePlayServicesClient". It give me an error "GooglePlayServicesClient cannot be resolved to a type". – Roland Sep 10 '13 at 12:08
  • here is what I have for my imports. – nPn Sep 10 '13 at 13:42
  • import android.content.Context; import android.location.Location; import android.os.Bundle; import android.util.Log; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient; import com.google.android.gms.location.LocationClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; – nPn Sep 10 '13 at 13:42
  • It's covered in the link above but you have to setup google play services http://developer.android.com/google/play-services/setup.html this is all a little more work than using the standard location provider scheme but it works nicely. BTW the twntee above is basically telling you the same thing (at least as far as using google play services which introduced the "fused" provider – nPn Sep 10 '13 at 13:48
0

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

Atish Agrawal
  • 2,857
  • 1
  • 23
  • 38