1

I have been trying hard to get my current location's GPS co-ordinates but my app never locks on to a GPS satellite.

The GPS icon in the notification area just keeps on blinking.

Whereas I tried using Google Maps on Android (the pre-installed app) and that thing is able to lockon in approx 60 secs! In both of the cases my 3G data connection was switched on and working.

Update: I am using Android 2.3.3 (HTC Desire S (Factory installed OS; no updates applied)) Logcat output is here. Now this is without setting LocationUpdates()'s min time and min-distance between update to 0, 0.

Update #2: My earlier code is here(PasteBin Link).

Update #3: Now, I am getting a force close after displaying a Toast .."Available".. in on onStatusChanged().

Update #4: Finally..I got it to work.

--
So, is it like that the Google map's app uses some proprietary code for locking on to GPS signals? I have tried using various version of my code. Tried using criteria(s) but never got them to work with GPS. For me getting precise (~50ft accuracy) location co-ordinates through GPS is a must.

My Code:

public class LocationDemoActivity extends Activity implements LocationListener {
    LocationManager locationManager;
    StringBuilder builder;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000l, 50.0f, this);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        locationManager.removeUpdates(this);
        locationManager = null;
        Intent i = new Intent(
                android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(i);

    }

    @Override
    public void onLocationChanged(Location location) {
//      builder = new StringBuilder();
        double lati=location.getLatitude();
        double longi=location.getLongitude();
        double alti=location.getAltitude();
        float acc=location.getAccuracy();
        float speed=location.getSpeed();
        long time=location.getTime();

        System.out.println(lati);
        System.out.println(longi);
        System.out.println(alti);
        System.out.println(acc);
        System.out.println(speed);
        System.out.println(time);

        /*Toast.makeText(this, "Lati: " + lati,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Long: " + longi,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Alti: " + alti,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Acc.: " + acc,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Speed: " + speed,Toast.LENGTH_SHORT).show();
        Toast.makeText(this, "Time: " + time,Toast.LENGTH_SHORT).show();*/


        builder.append("Longitide: " + location.getLongitude());
        builder.append('\n');
        builder.append("Latitude: " + location.getLatitude());
        builder.append('\n');
        builder.append("Altitude: " + location.getAltitude());
        builder.append('\n');
        builder.append("Accuracy: " + location.getAccuracy());
        builder.append('\n');
        builder.append("TimeStamp:" + location.getTime());
        builder.append('\n');
        System.out.println(builder.toString());

        Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        System.out.println("Provider Disabled:");
        Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

    @Override
    public void onProviderEnabled(String provider) {
        System.out.println("Provider Enabled:");
        Toast.makeText(this, "GPS is now enabled...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
        case LocationProvider.OUT_OF_SERVICE:
            System.out.println("Status Changed: Out of Service");
            Toast.makeText(this, "Status Changed: Out of Service",
                    Toast.LENGTH_SHORT).show();
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            System.out.println("Status Changed: Temporarily Unavailable");
            Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                    Toast.LENGTH_SHORT).show();
            break;
        case LocationProvider.AVAILABLE:
            System.out.println("Status Changed: Available");
            Toast.makeText(this, "Status Changed: Available",
                    Toast.LENGTH_SHORT).show();
            break;
        }
    }

}

Please do answer as it's quite urgent on me and any help is greatly appreciable :)

Thanks..

beerBear
  • 969
  • 2
  • 17
  • 41
  • In your tets did you have clear open view of the sky? did other applications got the GPS signal? – AlexWien Feb 04 '13 at 17:38
  • @AlexWien Thx for replying fast. Yes, I have a clear view of sky and the pre-installed Google maps (on my HTC Desire S) locks-on just fine(within 60secs on the very same spot). Is there a place where I can get a peek of Google's own code? implementation of Google map's GPS fixer ? – beerBear Feb 04 '13 at 17:40
  • This has nothing to do with Google maps, you probably set up your code params wrong. Does the onLocationChanged() output anythings to console? – AlexWien Feb 04 '13 at 17:42
  • Have you tried with 0 and 0 as minimum distance and interval, just to see if you get any coordinates at all? – Gjordis Feb 04 '13 at 17:45
  • @AlexWien I don't check it out what `onLocationChanged()` outputs to console LOL am standing out in the open without my laptop :D – beerBear Feb 04 '13 at 17:48
  • @Gjordis I will try that now :) Thanx for the headsup~ – beerBear Feb 04 '13 at 17:48
  • Hello, It happens to me with some of mobile device especially Old Android devices. Once I start using network, it helps me to get the location though I was not able to fetch using GPS.. I tried so hard, but no luck so far – Ramesh Sangili Feb 04 '13 at 17:55
  • @RameshSangili Well, the Google Maps is able to do that on my phone and on the very same spot I am standing at nearly a perfect level..so why not my app? I suspect Google has an enhanced codebase at the time of this android's 2.3.3's release. :| ? – beerBear Feb 04 '13 at 18:03
  • @Gjordis Nope..even setting the min. distance accuracy and time to 0, 0 isn't helping. Not able to lockon to a GPS satellite. – beerBear Feb 04 '13 at 18:27
  • Yes, it happens to me as well. Google maps shows the right place, but not from my application. I tried all possible way and left it off.. – Ramesh Sangili Feb 04 '13 at 18:31
  • @RameshSangili So, you're saying that if you switch on the GPS..and your Google Map's app gets a solid fix of location(using GPS)? The GPS icon appears stable and not blinking? :O On which Android version did you tried that? – beerBear Feb 04 '13 at 18:34
  • I tried on Android 2.2 and 2.3 India and Singapore devices, but I am in US with Android 2.3 and it works fine for me always – Ramesh Sangili Feb 04 '13 at 20:07
  • Beside Standalone GPS services, does your phone also have Google Location Services checked? If so unchecked it and try to see if Google Map app can get a fix. I think Google Map app uses both GPS and Network to get a fix. – Hoan Nguyen Feb 04 '13 at 20:08
  • @HoanNguyen ok..here it is.. I tried to fire up the google maps app (at that moment neither..the GPS nor the network was enabled) and it then asked for network to be enabled (#1 possibly wanting to access location using A-GPS)..but I enabled GPS instead :D and then tried to run google maps again..it then started to lockon to my position..I suppose network is definitely not a `required` criteria to get a fix using gmaps IMO..neither of both of them is.. :| (Also, I haven't really checked into Google's `Location services` being enabled on my device..probably they are enabled..) – beerBear Feb 04 '13 at 20:12
  • I had a brand new LG phone a few months ago. It could not get a GPS fix for an app I was developing. I turned off Google location services and Google Map app could not get a location fix through GPS either. But it would get a fix if I turn on Google location services. My guess is the GPS sensor in your device is broken if in the location setting of your phone the Google location services is unchecked and Google Map app cannot get a fix with only GPS checked. – Hoan Nguyen Feb 04 '13 at 20:45

3 Answers3

1

It seams pretty obvious that you won't get any Location updates, because you have set the minDistance to 50 meters or 164.042 ft. It appears you have confused this with accuracy.

The minDistance parameter is the minimum distance between location updates. So you would have to move at least 50 meters to get a location update.

Also make sure you have a clear view of the sky in order to have GPS signal.

Read more in the documentation

http://developer.android.com/reference/android/location/LocationManager.html

Alexandru Chirila
  • 2,274
  • 5
  • 29
  • 40
  • Fixed* :D yeah..actually it was a typo..Moreover I am banging my head against a wall as I feel there might be an issue with my device's 2.3.3 version. Don't have access to any other device so couldn't really test otherwise.. btw is there really a problem with 2.3.3 based devices in accessing GPS location? or is it just me? :| Are there more users who have faced problem which i am facing on 2.3.3? or HTC Desire S ? :( – beerBear Feb 04 '13 at 19:19
  • There are no problems in 2.3.3 or any version of Android SDK with the GPS location system. I have used them on more than one ocasion, on different devices without any problems. It seems strange at first, when starting out, that you wont get any new locations if not moving, but you have to understand that this is an hardware sensor, that will not memorize the last known location. – Alexandru Chirila Feb 04 '13 at 19:22
  • yep..but all I wanted to know is get a current location's fix and display it through a Toast :| Will try it on a different device or maybe tomorrow on a bright day..maybe sky will become a bit more clear tomm. :D – beerBear Feb 04 '13 at 19:28
1

I asked a related question here.

To me looks like your GPS on the phone is not able to see the sat's. You need to get our of your office/home onto open air. In my case, I moved to NetworkProvider since GPS was just too clumsy.

Also note, that the parameters you give for distance and time are not literal, its the best guess that the api makes. So dont count on response times/distances from the API callback.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148
0

Do you have all these manifest permissions?

ACCESS_COARSE_LOCATION

ACCESS_FINE_LOCATION

ACCESS_LOCATION_EXTRA_COMMANDS

ACCESS_MOCK_LOCATION

CONTROL_LOCATION_UPDATES

INTERNET
MrLeap
  • 506
  • 2
  • 11
  • Yes :) ` ` Have used both of them. And as you can see in the code my `requestLocationUpdates` is already accessing the GPS provider. – beerBear Feb 04 '13 at 17:42
  • I just added ` And got an error saying `Permission is only granted to system apps` ? – beerBear Feb 04 '13 at 17:46