What's wrong with this location code?

by Anna PS » Thu, 15 Apr 2010 23:35:23 GMT


Sponsored Links
 oe - I'm still struggling to write some code that can reliably get me
an accurate location fix.

I have written some code that I thought would (i) register with any
available location listener (ii) at onLocationChanged events, check
the location fixes for age and accuracy (iii) once a suitable location
fix was obtained, start an upload, and unregister the location
listeners.

However, it seems to get into a situation sometimes where
onLocationChanged isn't called at all. Yet in my log files, I can
still see the Android NetworkLocationProvider logging
onCellLocationChanged events. Is this a bug in my code?

The code and logs are below. Many thanks for your help.

LocationManager locationmanager = null;
LocationListener[] listener;
Location location;
private Double latitude, longitude;
private String latString = null, lonString = null;
long firstGPSFixTime = 0, latestGPSFixTime = 0;
int locAccuracy;
long locationTimeStored = 0;
public Boolean locationDetermined = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(LOG_TAG, "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.interstitial);
extras = getIntent().getExtras();
name = extras.getString("name");
//Check whether the upload is under way already - we don't want
things to upload multiple times...
if (savedInstanceState == null) {
upload_under_way = false;
} else {
upload_under_way =
savedInstanceState.getBoolean("upload_under_way");
}
// if it's not, attempt to get a location fix, and
show a notification
if (!upload_under_way) {
testProviders();
mNotificationManager =

(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
}

//
**************************************************************************
// Register with any available provider, and start checking updates
//
**************************************************************************

public boolean testProviders() {
Log.e(LOG_TAG, "testProviders");
String location_context = Context.LOCATION_SERVICE;
locationmanager = (LocationManager)
getSystemService(location_context);
Location lastLocation = null;
providers = locationmanager.getProviders(true);
listener = new LocationListener[10]; //horrible code
int i = 0;
//register with any available provider, and start
checking updates
for (String provider : providers) {
Log.e(LOG_TAG, "registering with provider " + provider);
listener[i] = new LocationListener() {
public void onLocationChanged(Location
location) {
Log.e(LOG_TAG, "onLocationChanged");
if (!locationDetermined) {



What's wrong with this location code?

by JP » Fri, 16 Apr 2010 07:46:08 GMT


 


Without diving into your code. Double check it by using GPS. GPS is
considerably more accurate than base station based triangulation,
which may not result in location changes even if you move a hundred
meters (in my experience, anyway).

--


Sponsored Links


What's wrong with this location code?

by Anna PS » Sat, 17 Apr 2010 06:41:23 GMT


 




The basic issue is this. I don't want to accidentally end up with a
LastKnownLocation that's days out of date, so I have to check the age
of the location fix.

Since the GPS time (from location.getTime()) and the system time can
be quite different (as <a href=" http://www.mail-archive.com/android- 
develop...@googlegroups.com/msg47517.html">discussed here</a>,
comparing the two doesn't work. The only reliable way seems to be:

- get an initial location from getLastKnownLocation
- wait for a location update from onLocationChanged
- compare the age of the two to make sure the latter is newer.

However, I'd also like to write code that handles gracefully the
possibility that the user is (say) indoors, and therefore won't get an
onLocationChanged event at all. (In which case, we should just take
the LastKnownLocation, and never mind if it's out of date.)

I seem to be at a logical impasse. What can I do? I can't wait for an
onLocationChanged event if it's never going to happen - my code will
hang forever! But equally I can't just take getLastKnownLocation every
time - it might be wildly inaccurate.

Maybe I need to run some kind of timer in the code, to check how long
we've been waiting for an onLocationChanged. Does that seem like the
best idea?

--



What's wrong with this location code?

by JP » Sat, 17 Apr 2010 09:06:43 GMT


 What I do.
I never even look at lastKnownLocation; as you state it might be old
like dustballs.
This means I will only process locations received fresh from
onLocationChanged().
As you note, this may never happen. So keep users informed
accordingly! If you couldn't acquire a fix, let the user know.








>



What's wrong with this location code?

by patbenatar » Sat, 17 Apr 2010 14:39:00 GMT


  have solved this problem [with much thanks to the brains on this
group] by running two LocationListeners side-by-side, one fine [GPS]
and one coarse [Network]. The first provider to get a location update
wins and I use that location. This works for me as I don't care about
the location being super accurate at this point [later in my app I
allow the listeners to run their lives and whenever GPS is active and
available I use its updates then if it goes unavailable/disabled I
allow Network to take over [the whole while I'm running the two
listeners side-by-side, and just not handling updates from Network if
GPS is active/available].

You really have no reason to have a LocationListener[10] array...
There are really only two location providers, Network and GPS. So all
you need are two LocationListeners: fineLocationListener and
coarseLocationListener.

Here, this is the thread in which I learned how to do this:
http://groups.google.com/group/android-developers/browse_thread/thread/99ebef7294b094d5/6945dab119f1bf67
.. its a pretty good discussion on LocationListeners.

Hope this helps!

-Nick


On Apr 16, 6:06pm, JP <joachim.pfeif...@gmail.com> wrote:
> >



What's wrong with this location code?

by patbenatar » Sat, 17 Apr 2010 15:22:20 GMT


 h, also... If a GPS fix is all you will accept, firstly your app will
not work in big buildings, under tunnels, dense metro areas [San
Francisco, NYC, etc] or anywhere like that where 3 GPS satellites are
not accessible. Secondly, if you can't get a fix after some sort of
realistic timeout, you'll need to notify the user of this so they're
not waiting around while your app repeatedly fails to get a GPS fix.

To expand on my above post, I would definitely recommend using both
listeners side-by-side but just always giving precedence to GPS
updates. This fixes many problems: 1) You'll pretty much always be
able to get some sort of fix. 2) In cities where GPS may unavailable,
there are likely lots of cell towers so cell triangulation will be
quite accurate [In Los Angeles, I've seen a Network fix be merely tens
of meters off from a GPS fix]. 3) In rural areas where cell towers are
few and far between, GPS is likely accessible [not many tall
buildings] so you'll be able to use that accurate fix.

-Nick



On Apr 16, 11:38pm, patbenatar <patbena...@gmail.com> wrote:
> > >



What's wrong with this location code?

by Michael Thomas » Sun, 18 Apr 2010 06:54:05 GMT


 

You really have no reason to have a LocationListener[10] array... There are really only two location providers, Network and GPS. So all you need are two LocationListeners: fineLocationListener and coarseLocationListener.
Perhaps you are forgetting about the chain and theodolite provider. Mike ;-) --



What's wrong with this location code?

by patbenatar » Sun, 18 Apr 2010 09:56:44 GMT


 > Perhaps you are forgetting about the chain and theodolite provider.

Mike-

What are those? The LocationManager class only has these two provider
constants: GPS_PROVIDER, NETWORK_PROVIDER.

Thanks!
Nick








>



What's wrong with this location code?

by Michael Thomas » Sun, 18 Apr 2010 23:16:35 GMT


 

Perhaps you are forgetting about the chain and theodolite provider.
Mike- What are those? The LocationManager class only has these two provider constants: GPS_PROVIDER, NETWORK_PROVIDER.
I was just making a funny, but the larger point is to not code things as if these are the only two forever more, (think ultimately Galileo as an example), and certainly don't make assumptions about the accuracy of resolution (Galileo is supposed to be quite a bit more accurate once its constellation is up). Mike
Thanks! Nick
You really have no reason to have a LocationListener[10] array... There are really only two location providers, Network and GPS. So all you need are two LocationListeners: fineLocationListener and coarseLocationListener.
Perhaps you are forgetting about the chain and theodolite provider. Mike ;-) --



What's wrong with this location code?

by Maps.Huge.Info (Maps API Guru) » Mon, 19 Apr 2010 00:24:12 GMT


 Personally, I wouldn't be making any coding plans for using the
Galileo system for location handling any time this decade. It will be
amazing if the EU manages to organize it into a working system in any
lifetime of current OS's or apps for that matter. Think of how long it
took to get a working engine for their A400 program, nearly 20 years
and the darn thing still isn't anything but a test program. The
Galileo system currently has no "in operation" date and only two test
satellites have been launched, none of them will be used in an
operational program. A functional system is at least a decade away
optimistically. I think you can safely ignore that possibility as a
means of location detection on current Android devices.

-John Coryat

--



What's wrong with this location code?

by patbenatar » Mon, 19 Apr 2010 16:15:45 GMT


 Mike-

Hahah--Word. I very much respect your position, although I do have to
agree with John here... And as far as other possible location
providers popping up anytime in the near future, new versions of the
OS will have to accompany them, along with new versions of the SDK,
which would prompt me, as an active developer, to update my apps, and
thanks to the Market's auto-update-prompt feature my users will
receive my update. I'd rather launch a current app faster and upgrade
it as new features become available than spend more time on initial
development planning for future features that may never exist. It is
indeed quite legit to plan for the future as you're talking about,
however. For me it's just a balance of pre-release development time
vs. post-release dev time.

-Nick




On Apr 18, 9:23am, "Maps.Huge.Info (Maps API Guru)"


>



Other Threads

1. Anyone with GL experience willing to take a contract/bounty?

We have some game ideas we want to try developing using the android GL 
libraries.
However GL programming is very different from normal app development.

We want to find someone who will contract or answer a bounty for some 
extensions to one of the better game engine libraries.
We'd like to use something like GDX but would also be just as happy with 
Angle or AndEngine. 

We would be fine for any of the code we pay for to be submitted to those 
engines as enhancements (i.e. we are not asking for exclusive rights etc).

Essentially we are looking for ways to simplify our development so we can 
focus on what we want to do, rather than on how we get it to work.

Just send me a private message if your interested and we'll sort out how its 
going to work in a week or two when we've given people time to answer.

-- 

2. Pre DEX optimization Info

Hi,
I am trying to use DEX Pre optimization for andoird 2.2_R1 (Froyo), when I
guild the froyo code as below:

  $ make WITH_DEXPREOPT=true -j4

I dont see any ODEX files getting generated for the APKs.

When I do the same thing for 2.3 (gingerbread) , I can see ODEX files
getting generated for each APK.

Also when I checked the build system for 2.2_R1 , It does not have changes
to build ODEX files.

Can someone let me know if the DEX Pre optimization is supported only in
Gingerbread?

Or if its supported in froyo is there a way to get the ODEX files?

Thanks & Regards,
Channagoud

-- 

3. Kamu pembohong

4. internal Mem di android

5. [WTB] Motorola Backflip or HTC My Touch 3g Slide

6. [WTShare] How Cool Can a Flip Phone Get?

7. 5 Ponsel Android Termurah