android location update problem - android

I have an activity that needs to display an alert to user when location is updated,
but i need first to get current location (update last known location) and then start a location update listener to display alert in this activity.
Basically what i need is to display an alert if location changed from time activity was created.
If i use getLastKnownLocation when activity is created, it might be outdated, then the requestLocationUpdate listener is called a few seconds from activity creation time.
How can i do this?
Thanks in advance,

Getting last known location (quickly)
LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
from ( http://developer.android.com/guide/topics/location/obtaining-user-location.html#FastFix )
Listen for location updates: http://developer.android.com/guide/topics/location/obtaining-user-location.html#Updates
This is the functionality you can use. But know that last location might be pretty old and inaccurate. Depends on when a application was trying to get the location the last time. But it normally should be somewhere in the area you are.
If that is not good enough, than you will have to do something like this:
start listening to updates
save the answers for the first 1-3 times or so. (didn't test what would be best. maybe even higher). or maybe for 1 minute or 5 minutes (gps can take a long time if you need the location exactly)
after that you do not save the location updates anymore but check if it moved.
I hope this helps.

Check Blow Link..
May be it's help you...
In that u have to pass sach argument so that Location manager can easily Update Your Location and your code is also right to get Location ....
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28long,%20float,%20android.location.Criteria,%20android.app.PendingIntent%29

Related

Last known location too old

I'm new with the Google's Fused Location API and I would like to know if there is any way of requesting a location update in any given time?
I know about setInterval() and setFastestInterval() but For example: If the user presses a button, can I get THE current location and not the last known? I just don't want to have the app working at very short intervals to conserve battery.
Thanks
getLastKnownLocation() only returns the last fix. So if no location providers are being updated the return value of getLastKnownLocation() will not change. The location object will also provide you with accuracy and time of the fix.
I would look at this post for more information. You could use some version of the one shot location.
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html

Android LocationManager.getLastKnownLocation()

From android documentation LocationManager.getLastKnownLocation():
Returns a Location indicating the data from the last known location
fix obtained from the given provider.
Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
How often provider updates device location ? How its works ?
The reason why I asking is that I dont want to use locationListener,
I just need to get current geo location on button click, and thats it.
Can I just do like this ?
final LocationManager mlocManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
final Location currentGeoLocation = mlocManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
How often provider updates device location ? How its works ?
It depends, and you may never know. If a device has good reception then updates about the user's location will fire rapidly.
Can I just do like this ?
Not really. If that is firing too soon then you will not get any location back. If the user doesn't have great reception then it could take 10 seconds before you have a reasonable idea of the user's location. I think you'll have to approach this slightly differently. I'm afraid I must recommend locationListeners :P
if you absolutely can't wait for a location you can try getLastKnownLocation(), but you need to be prepared to have a NULL result.
getLastKnownLocation() is just a cache of the last location from the provider you specified. In some cases there won't be one at all, like when the phone just rebooted. Even when there is a cached location it may not be accurate.

GPS is not working inside the building

After few hours of testing outside of house, when i came back to my house i found that GPS is enabled but was not getting location fixes inside the building.Hence its onLocationChanged method couldn't get called.
Problem: How to know that GPS is not getting any location fixes as device continue to sense your location in "trying mode".By trying mode i mean the situation where it is not coming to any result even after 20 to 30 minutes still it declared it self as enabled (blinking in status bar).
How one could know that the GPS doesn't get location so switch to another provider like Network_Provider.
In short i want to get my device to conclude something that GPS can find location fix for sure or you have to take location by another means.
I hope at least someone can give me idea about how to deal with that.
The link below has an awesome tutorial, of how to get the location from GPS and/or Network.
It uses a timer task, which analyzes if there is a GPS location in a specific period of time, assume 20 seconds. If not, it will return the location from Network as the current location. If there is a location from GPS, then it will compare which update is new (latest), and return that.
What is the simplest and most robust way to get the user's current location on Android?

How to call to get a single gps fix when ever i want android?

How can I get a single GPS fix of my location just by calling one function? Just for this example how can I get the Lat and Lon into a toast.
What have you tried?
Here are two of the official pages for location: http://developer.android.com/reference/android/location/Location.html
http://developer.android.com/reference/android/location/LocationManager.html
But to get the most recent location and show it in a Toast:
LocationManager locMan = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
String lat = String.valueOf(loc.getLatitude());
String longitude = String.valueOf(loc.getLongitude());
Toast.makeText(context, lat+","+longitude, Toast.LENGTH_LONG).show();
If you want to request a new location, it is more code, and I will not cover that here. You can do some google searching or searching on here to find how to use it. The method you need is requestLocationUpdates or requestSingleUpdate. I'm guessing you would prefer requestSingleUpdate
I do not think that you can just make one call and get a location fix. GPS / Android just doesn't work that way. The phone may have a last known location, but that last known location may have been taken/recorded hours ago and miles away. The location returned by the getLastKnownLocation() method has a time stamp and an accuracy that can be used to see if the location is "good enough".
Locations are typically determined by setting up a listener, listening for updates and stopping the listener when you have a good enough fix. See Obtaining User Location for a good worked example, especially the details in the example isBetterLocation() method.
I find it best to create an Asynchronous task that actively registers and deregisters GPS/network/passive listeners depending on whether the application has a good enough location, or not, and have that class export a getMyPosition() method that returns a Location object if a good enough position has been established, or null if not. Then the main code can make a simple one line function call to get the current position. But only because there is an asynch task behind the scenes doing the hard work.
I try to make my asynch task actively deregister the GPS listener and turn off the GPS circuits to save battery live when I have a good enough fix. How long I turn off the listeners depends on the needs of the application. Leaving the passive listener left on (registered) allows my task/application to listen into the GPS position reports caused by any other application on the device "for free".
Getting a good enough position is not a one function call deal, unless you make it so with lots of behind the scenes work.

Displaying current location in android

How to display current location with out using onLocationChanged method
You could use LocationManager.getLastKnownLocation(), but that will only give you the last known location - which might be nothing (if the user just turned the phone on), or someplace hundreds of miles away (if the last time they turned a location service on was far away).
If you really want to know where the user is right now, you're going to have to implement a LocationListener.
Use requestLocationUpdates() and when you reach onLocationChange, stop it. That what my program does, this way you ensure that once the new location is gotten it will stop looking for updates.

Categories

Resources