Displaying current location in android - 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.

Related

How to distinguish between no location update and no location fix in Android?

I have an Android app where I should somehow clearly distinguish between whether user has not moved for an amount of time or location was not resolved during that interval. As I understand in both cases I will not receive any update if I requestLocationUpdates().
Does anybody has an idea on how to implement this?
The sample case is:
I've got 2 valid and accurate locations: 1 from GPS and 1 from network; Good, that's exactly what I need.
One minute has passed: the user may have gone to a kind of underground station and lost gps/network (and ability to resolve location) OR may have not moved at all;
Now I have to decide whether the location is perfectly relevant (user not moved) or I have no location at all (all providers lost). But how? Seems like in both cases I receive no location updates.
Try using LocationClient.getLastLocation. You can store this location and check against it later.

How to wait for a GPS fix before recording location

I am having trouble finding an approach to wait for a GPS fix in my Android application. I am running a service to record user location and want to wait for a GPS fix before I do so. Can anyone suggest a method for approaching this?
Just get a locationManager and request location update for GPS. Do not call getLastKnownLocation. When you get the onLocationChanged() that is your GPS fixed.
This is indeed one of the critical things.
You will have the problem, that either you could have an not accurate fix, or you are waiting for an acurate one, and the user does not get an position feedback in your app.
This much depends on your app.
You can wait until the location.getAccuarcy() is under 30m, or the first one that have a speed over x km/h, for application swhere you want to record movement.
To show on a map, you want to take the first you get.
There is no universal soultion for this.

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.

Android : Getting CURRENT coordinates (not lastKnownLocation)

Right now, I only know of one method to do this:
- Get last known location
- Have the location manager request location updates
However, I really only need to get the CURRENT coordinates ONCE right when the application is called, but it's not doing what I want.
What's the simplest way to get the current coordinates? Is there something I could call or some code I could use just to get the location RIGHT NOW ?
thanks in advance! I'm still a little new with android development.
What's the simplest way to get the current coordinates?
There is no way to get the current coordinates on demand.
Is there something I could call or some code I could use just to get the location RIGHT NOW ?
No, for three related reasons:
Not all location technologies are low power. GPS, for example, is a serious battery hog. Hence, GPS is not powered on unless something is actively seeking a GPS fix.
Not all location technologies are instantaneous. GPS, for example, takes some number of seconds to get a fix after being powered on.
No location technology is universally available. GPS, for example, may be unavailable because you are in a large building.

Categories

Resources