Android Question GPS library - last known position

pjetson

Member
Licensed User
Longtime User
I seem to have found that if a device with a GPS was outdoors and working, and then was brought indoors where there is no signal, and then my app is run, my app will not be able to get even the "last known" GPS co-ordinates, because there has been no GPS_LocationChanged event since my app started listening to the GPS, and therefore the Location object will not contain any information.

Is that correct? Is there any other way I can determine the last known position in this circumstance?
 

Beja

Expert
Licensed User
Longtime User
If the connection is lost then nothing will work.. the best bet is to use a timer and log the last coordinates in the same label, overriding the last reading. If you don't like to use a timer, then use the LocationChanged event to log same.. in this case you may want to ignore the last few digits from each coordinate, and only check changes in the bigger digits (for longer periods between logs).
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
Hi, Beja. Unfortunately, if my app was started after the GPS has stopped moving, my app wouldn't have been running to be able to get the GPS location before the GPS stopped moving :)

Basically, my application needs to log where the user is periodically. It does other stuff, but that's not important here.

If the GPS is not moving when my app is started, it seems that I can't even get the last known location of the GPS because the GPS is not currently moving.
 
Last edited:
Upvote 0

pjetson

Member
Licensed User
Longtime User
Thanks, Erel - I know that GPS does not work indoors, but in my application, even the last known location where it was working would be an advantage.

I will search for the libraries that you mention.
 
Last edited:
Upvote 0

pjetson

Member
Licensed User
Longtime User
That wouldn't give me the last known location, it would only give me the location the last time my program was run, which in my application would be very different, and not useful.
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
You need of course to also update the latest location in the GPS1_LocationChanged routine !
Or you need to give more details on what exactly you want to do.
Hi, Klaus. The GPS1_LocationChanged routine doesn't return anything if there is currently no GPS signal and the app is started after the signal loss. It would be helpful to me if it retained the last known location, but that's not how it works.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
pjetson,
If you carefully followed what Klaus detailed then you can easily solve the problem.
Again, and please pardon my oversimplification:

1- You are outside the App is working fine.. your app will use the locationchanged event, take the coordinates and save it in a text file.
2- Now at any time you already have the LAST coordinates saved in a text file, because each locationchanged event will update the text file
3- Now you are inside and the app is not working -fine
4- Now you are out again.
5- You must program your app so that whenever it starts (Activity create or whatever restarts the app), you must first check the file and
see if it contained a valid value that represents geo coordinates, use those values and start from there.

I hope the idea is clear.
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
Thanks, Beja, but if the app is not running in your point 1, it will not be able to save a location.

I think the point that is being missed is that the app may not be running 24/7. I've tried to say that in all my posts above, but apparently I haven't been clear enough.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
If the app is not running in point one, then how on earth are you going to have any location info whether they are last or first?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What you need is:
- A process global variable which holds the last known location.
- A timer to read the last known location at given time intervals.
- The last known location variable must be updated in GPS1_LocationChanged.
- Save the last known location in a file in Activity_Pause and stop the timer.
- Read the last known location from the file in Activity_Create and enable the timer.
This works as soon as you have the first GPS location.

You should also have a look at these two libraries:
- LocationManager Library
- Expanded Location Manager Library
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
What you need is:
- A process global variable which holds the last known location.
- A timer to read the last known location at given time intervals.
- The last known location variable must be updated in GPS1_LocationChanged.
- Save the last known location in a file in Activity_Pause and stop the timer.
- Read the last known location from the file in Activity_Create and enable the timer.
This works as soon as you have the first GPS location.

Hi, Klaus. This still seems to require the app to be running all the time. If I could be sure that the app was always running, I wouldn't have this problem.
You should also have a look at these two libraries:
- LocationManager Library
- Expanded Location Manager Library

The use of other libraries is the avenue that I am exploring at the moment. Erel says that there are libraries that will expose the last known location.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Because the GPS in the phone is running.
the built-in GPS in the phone is not an app.. it is not an application.. it is a device just like RF radio or Bluetooth module.. your app uses it to grab location information from the satellites.. consider it like antenna not more, and doesn't save any information..
The last message from Klaus clearly gives you the steps to solve the problem as you described.
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
the built-in GPS in the phone is not an app. it is not an application. it is a device just like RF radio or Bluetooth module.
Of course the GPS is not an app.
your app uses it to grab location information from the satellites.
And that, Beja, is the problem. My app is not running at that point!

And because my app is not running, Klaus's steps will not solve the problem, because there is no app running to listen to the GPS, no app running to get the results from the GPS1_LocationChanged event, no app running to set a timer, and no app running to save the GPS location.

Klaus's steps will not solve the problem because there is no app running to execute the steps he is describing!

I'm sure you agree that if my app is not running, it can't do what Klaus suggests.

My app is not running 24/7. It cannot obtain the last known position from the GPS because it was not running at the time that the GPS last reported a position via the GPS1_LocationChanged event. Therefore, Klaus's suggestion cannot be implemented.

The last time that the GPS received a signal, the GPS knew where it was. And if my app had been running at that point, my app would also know where it was. And it would be able to save that position, just like Klaus is suggesting. Unfortunately, Beja, my app is not running!

As I said in my very first post, if a phone with a GPS is turned on and the GPS is enabled, and the phone is then taken inside where there is no GPS signal, then any app relying on the GPS1_LocationChanged will not be able to get a location reading from the GPS. I have proven this occurs, it's not just theoretical.

Imagine that my app is installed on your phone. Imagine that my app is not running. In fact, imagine that my app has never run on your phone. How does Klaus's code get run?

Okay, now imagine that the GPS was enabled on your phone. Imagine that you are outside your house, and that the GPS is receiving a signal. Now, go inside your house. Most likely, your phone's GPS will not receive a signal inside your house. Now, run my app. Do you agree that my app will not be able to obtain its location from your phone's GPS? Do you also agree that since there was no app running Klaus's code, that the results from that code will not be available to my app?

Hopefully, we're on the same page now. I apologise for the length of this post.
 
Upvote 0
Top