Trouble Coding GPS

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Trouble Coding GPS

Postby TuxBobble » Fri May 07, 2010 9:35 pm

Maybe someone here can point me in the right direction. I'm trying to make a GPS-location-based application, and having trouble with the GPS aspect of the program.

Currently, I have one main .java file containing a class that implements GpsStatus.Listener, LocationListener, etc.

Basically my program is: (apologies for any typos--I'm re-typing it on one screen from the other due to currently being unable to transfer a file from the source-containing computer--long story)

Code: Select all

//In my onCreate

locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
if(locationManager.getProvider("gps" != null)
{
    gpsStatus = locationManager.getGpsStatus(null);
    locationManager.addGpsStatusListener(this);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,this);
}
else
{
    failedFunction();
}


Code: Select all

//my listener for onLocationChanged

public void onLocationChanged(Location loc)
{
    double accuracy = loc.getAccuracy();
    if(accuracy > 0)
    {
        if(accuracy < 75)
        {
            successFunction();
            return;
        }
    }
}


The problem right now is that I don't seem to be getting into onLocationChanged and accuracy between 0 and 75 very often. (I tried changing the upper bound to 200 at one point too, and that didn't seem to help)

Accuracy seems to be 0 when it has yet to find my location, so I needed the > 0 in there, I think. I often get the onGpsStatusChanged(int event) method called, but I never seem to get my accuracy in that range. (I did once or twice, but it seems very very rare) For the record, the 200 is an arbitrary number really, but I know using Google Maps I've gotten WELL UNDER 200 accuracy, in the same locations I seem to not get accuracy at all, at the moment. (I also haven't created virtual coordinates yet--that is the next thing I'll look into if I can't solve this problem)

I might just not understand the way GPS events are handled, because I'm pretty new to Android. (and also to Java--I'd done it in the past but it was a basic class and a loooong time ago) If you have any tips, please let me know.

P.S.-I am also hoping to add a "timeout" control to this GPS functionality. So that, for instance, if a location can't be found within X seconds (or Y attempts) then the program will do something else based on GPS failure. I was just using an iterative approach, where if(i > Y){failedFunction();} but I was trying to find a more elegant solution than this...

Thanks in advance for all the help. Love the AndDev forums!
TuxBobble
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Dec 30, 2009 7:36 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Google [Bot] and 1 guest